如何在Symfony 1.4 Propel中制定SELECT CASE的条件

问题描述 投票:0回答:2

我正在寻找一种基于Symfony 1.4中的SQL制定标准的方法:

SELECT item.position, 
CASE 
    WHEN item.position = 1 THEN item.position + 1
    WHEN item.position = 2 THEN item.position - 1
    ELSE item.position
END AS new_position
FROM item ORDER BY new_position ASC

我正在尝试使用this post中的示例,但这无济于事

制作$criteria很重要,因为它稍后会传递给呈现项目列表并按$criteria对项目进行排序的插件。

WHEN条件仅是示例,我将使用更复杂的条件。

php symfony-1.4 criteria propel
2个回答
0
投票

您可以在Query.php类中使用php开关来建立标准(由于您的问题显示了简化的示例,因此您的标准所需要的开关可能要复杂得多:]

//in YourModelQuery.php
public function addMyItemPositionCriteria($itemPosition) {
    //TODO: validate $itemPosition values as needed
    switch ($itemPosition) {
        case 1:
            return $this->add([$criteria see http://api.propelorm.org/1.6.8/ browse propel.runtime.query Criteria and ModelCriteria classes ]);
            break;
        case 2:
            return $this->add([$criteria here]);
            break;
        }
}

或者您可以类似地在ModelPeer.php类(或没有函数调用的动作类)中构建$ criteria对象:

// in MyObjectPeerClass.php
public static function addMyItemPositionCriteria($itemPosition) {
    //TODO: validate $itemPosition values as needed
    $criteria = new Criteria;
    switch ($itemPosition) {
        case 1:
            return $criteria->add([$criteria see http://api.propelorm.org/1.6.8/ browse propel.runtime.query Criteria and ModelCriteria classes ]);
            break;
        case 2:
            return $criteria->add([$criteria here]);
            break;
        }
}

然后您可以使用此条件:

//depending on context
// in ModelQuery.php call 
    $this->addMyItemPostion($itemPosition);
// call to model query
    ModelQuery::create()->addMyItemPosition($itemPosition)->find();
//in action or somewhere else (adding to previously defined $criteria)
    $new_criteria = MyObjectPeerClass::addMyItemPosition($itemPosition);
    $criteria->add($new_criteria);

0
投票

您可以使用语句“ withColumn”,然后自定义查询

-> withColumn(“ CLIENTE_ID = {$ cliente-> getId()}的情况,然后'Pontos Pessoais'ELSE'Pontos de Equipe'END”,'descricao')

© www.soinside.com 2019 - 2024. All rights reserved.