Yii CDbCriteria和OR

时间:2014-08-25 13:28:38

标签: php yii

$criteria = new CDbCriteria();


        $criteria->addCondition('admin_status = ' . Transaction::ADMIN_STATUS_WAITING);
        $criteria->addCondition('status = ' . Transaction::ORDER_STATUS_WAITING);
        $criteria->addCondition(
            'DATE_SUB(CURRENT_DATE(), INTERVAL ' . $this->period . ' DAY) > DATE(order_date)'
        );
        if($this->type != null) {
            $criteria->addCondition('type = ' . (int)$this->type);
        }
        $criteria->addCondition('admin_status = ' . Transaction::ADMIN_STATUS_REJECTED . ' AND  status != ' . Transaction::ORDER_STATUS_REJECTED, 'OR');

我现在有什么:

(((admin_status = 0)AND(status = 0))AND(DATE_SUB(CURRENT_DATE(),INTERVAL 120 DAY)> DATE(order_date)))OR(admin_status = 2 AND status!= 2 )

我需要的是:

(((admin_status = 0)AND(状态= 0))AND(DATE_SUB(CURRENT_DATE(),INTERVAL 120 DAY)> DATE (order_date))) OR(admin_status = 2 AND status!= 2)

如何将3个第一标准加入一个?

我需要(criteria1 AND criteria2 AND criteria3 ) OR (CRITERIA 4)

2 个答案:

答案 0 :(得分:2)

基本上,只需在一个条件下完成所有操作。即使他们是类常数,我仍然建议使用参数绑定。

$criteria = new CDbCriteria();
$criteria->addCondition('((((admin_status = :admin_status_waiting) AND (status = :order_status_waiting)) AND (DATE_SUB(CURRENT_DATE(), INTERVAL :period DAY) > DATE(order_date)))) OR (admin_status = :admin_status_rejected AND status != :order_status_rejected)');
$criteria->params = array(
    ':period' => $this->period,
    ':admin_status_waiting' => Transaction::ADMIN_STATUS_WAITING,
    ':order_status_waiting' => Transaction::ORDER_STATUS_WAITING,
    ':admin_status_rejected' => Transaction::ADMIN_STATUS_REJECTED,
    ':order_status_rejected' => Transaction::ORDER_STATUS_REJECTED
);

答案 1 :(得分:1)

如果您已正确检查,我认为它已经是您想要的了

(
           (
           (admin_status = 0) AND (status = 0)
           ) AND (DATE_SUB(CURRENT_DATE(), INTERVAL 120 DAY) > DATE(order_date))
) OR (admin_status = 2 AND status != 2)

你什么都不做,只想添加一对额外的括号。