Filter / Search Empty Column Cgridview

I joined a table now I have some empty columns, which is perfect because I want to search and display those empty columns. The only problem is how do you search for an empty column on CGridView because it’s not 0… I tried

'value' => '(empty($data->ID)) ? "0" : $data->ID' ,  // I want to show all data with "0"

But it only searches models, so any text that’s not in the DB cannot be searched. if I search 0 I get no results.


Dear ItsYII

We can overide the afterFind method in AR.

public function afterFind()









@Jimlam and @seenivasan thank you all for the replies

Both solutions produce the same results I already have, it prints 0 on the column but it cannot be searched…


Is ID an attribute of the model being searched? I guess that it is not. It is an attribute of the related table.

I think this wiki can help you:

Can it be both :o ID is an attribute of the model yes, but the empty ID’s are the attribute of the related table. I read the Wiki and it mostly explains joining tables wich I successfully have, nothing to do with searching empty fields. I suppose you are right because the one I’m trying to search is an attribute of the related table. I can search through all related table attributes just fine, only empty values I can’t.


it can be done with something like this in your search method:


      $criteria->addCondition('column is null');



Dear ItsYII

You are absolutely correct.

And the solution is elegantly given by Reza M.




		            $criteria->addCondition("ID IS NULL OR ID=0");

		        else $criteria->compare('ID',$this->ID);



Yep that’s done it, thank you very much.

Appreciate all the help from everyone

If you need to do this for every field in your model you can use the attributelabels() function to get it done in a very easy way:

foreach ($this->attributeLabels() as $key => $value) {

      if (strtolower($this->$key)=='0')

         $criteria->addCondition($key.' is null');


         $criteria->compare('LOWER('.$key.')', strtolower($this->$key), true); //last parameter is optative