Thu Ra
Thu Ra

Reputation: 2063

Yii : how to add "OR" operator at $criteria?

Q : How to add "OR" operator at $criteria?

reference from here

This is one of my $criteria condition.

$criteria->addSearchCondition('t.status','Reviewing',true,"OR","LIKE");
$criteria->addSearchCondition("status",'On Hold');

this is the sql from $criteria

SELECT COUNT(*)
FROM `request` `t`
JOIN users u ON (t.creator = u.id)
WHERE ((t.status LIKE :ycp0) AND (status LIKE :ycp1)) AND (reviewers LIKE :ycp2)

This condition ((t.status LIKE :ycp0) AND (status LIKE :ycp1)) should be ((t.status LIKE :ycp0) OR (status LIKE :ycp1))

Upvotes: 0

Views: 9189

Answers (2)

hakre
hakre

Reputation: 197692

I'm not that fluent with YII, but your second criteria:

 $criteria->addSearchCondition("status",'On Hold');

Appends a search condition to the existing condition. The search condition and the existing condition will be concatenated via the specified operator which defaults to 'AND'. The search condition is generated using the SQL LIKE operator with the given column name and search keyword.

Does not specify it should be OR'ed, but by default is AND'ed. It probably will already do it for you to specify OR there:

$criteria->addSearchCondition("status",'On Hold', true, 'OR');

Also you do not need to use a search condition here, you just want to compare. Same for the t.status condition. So you're probably more easy with a more straight forward criteria like addColumnCondition:

$criteria->addColumnCondition(
    array('t.status' => 'Reviewing', "status" => 'On Hold'), 'OR'
);

This could replace your two lines I think.

Upvotes: 2

GBD
GBD

Reputation: 15981

I think you need to change as below

$criteria->addSearchCondition('t.status','Reviewing',true,"AND","LIKE");
$criteria->addSearchCondition("status",'On Hold','true','OR');

addSearchCondition function append operator to previous condition

Upvotes: 3

Related Questions