Reputation: 3661
Currently my code looks like that
$criteria = new CDbCriteria();
$criteria->compare('visible', 1);
$criteria->compare('branch_id', $this->id);
What I want is to do something like that
`branch_id`='$this->id' OR `branch_id` is NULL
How to do it with compare
function?
Upvotes: 1
Views: 8683
Reputation: 1120
So maybe you'd like to do
$idCrit = new CDbCriteria();
$idCrit->compare('branch_id', $this->id);
$idCrit->addCondition('branch_id is NULL', 'OR');
$criteria->mergeWith($idCrit);
Where $criteria
holds all the other stuff?
Some further reading;
(Look specifically at the $operator
argument.)
Upvotes: 4
Reputation: 35
use this:
$criteria->compare('branch_id', array($this->id, null));
as you can see in code:
https://github.com/yiisoft/yii/blob/1.1.14/framework/db/schema/CDbCriteria.php#L414
"If the value is an array, the comparison is done by exact match of any of the value in the array."
Upvotes: 0
Reputation: 2060
This works for me:
$criteria->compare('field', array(NULL));
Upvotes: 0