heron
heron

Reputation: 3661

Using condition or in cdbcriteria

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

Answers (3)

Alexander Kuzmin
Alexander Kuzmin

Reputation: 1120

Yii condition <IS NULL>

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

Shahram
Shahram

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

Amir Hossein Jamsidi
Amir Hossein Jamsidi

Reputation: 2060

This works for me:

$criteria->compare('field', array(NULL));

Upvotes: 0

Related Questions