Filter For Null In Cdbcriteria


i have a mysql table like this:

id| name |_city







the city-col can be null.

when i use cdbcriteria for filtering the records with city i have done something like this:


     'condition' => 'city = :city',

     'params' => array(':city' => $city)


now i want filter for a city (can be NULL!!).


  • $city=‘London’ result {John,Brian,Jack}

  • $city=‘Berlin’ result {Nick}

  • $city=NULL result {Eva,Jane}

the problem is, this does not work with NULL values becasue mysql use different syntax to check NULL-values:

  • normal => city="London"

  • with NULL => city IS NULL

any idea how i can do this in a nice way?

so why don’t you just write it in ‘condition’ expression:


     'condition' => 'city = :city OR city IS NULL',

     'params' => array(':city' => $city)


the problme with this OR is, i will get EVERYTIME the NULL-fields.


$city=‘Berlin’ result {Nick,Eva,Jane}

Nick (city = :city) + Eva,Jane (city IS NULL)

check this


     'condition' => 'city = :city',

     'params' => array(':city' => NULL)


this will work

whats the problem with $city ?

IS NULL not needed.

ah, ok. so you could use conditional:

$criteria = $city == null ? array( 'condition' => 'city = :city', 'params' => array(':city'=>$city) ) : array( 'condition' => 'city IS NULL' );


No, in MySQL at least, null comparisons must be performed with the IS keyword. Using ‘=’, null compares against anything (including null) as null, so won’t return the records.

You could use this slightly hacky solution:


     'condition' => $city === null ? 'city IS :city' : 'city = :city',

     'params' => array(':city' => $city)


Otherwise, you could simply use two different find statements, dependent on whether $city is null.


Ah, ninja’d…

i was hoping for a more smart solution, but i think this your proposal is a good idea, thanks :smiley:

i do not really unterstand why MySQL use this strange NULL-value-handling

so i tihnk i will use this:


     'condition' => $city === null ? 'city IS NULL' : 'city = :city',

     'params' => array(':city' => $city)


The behavior is at least defined very clearly here.

I think the whole point is that NULL is undefined, so it makes no sense for a standard comparison to compare two NULL values as equal. It’s undefined, so the comparison is essentially undefined.

Also, your version isn’t quite right, because you’ll require no parameters for the null version and one for the non-null version, but you always provide one parameter. That’s why I bound the variable in both conditions.

oh you are right… thanks


$this->findByAttributes(array('city' => $city));

It will both handle NULL values properly and do parameter binding automatically.

thank you!

I just looked at the class CDbCriteria (if you want to use CDbCriteria

the code says you can use

$criteria->addInCondition(‘city’, null);

The original class (snippet) is this:

[i]public function addInCondition($column,$values,$operator=‘AND’)



		&#036;condition='0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE





			&#036;condition=&#036;column.' IS NULL';