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.

Thanks

Dear ItsYII

We can overide the afterFind method in AR.




public function afterFind()

    {

	parent::afterFind();


	if(!isset($this->ID))

	    $this->ID=0;

   }



Regards.

Try




'value'=>'$data->ID!==null?$data->ID:"0"




@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…

Cheers

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:

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

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.

Thanks

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




if($this->column=='0')

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

else

      $criteria->compare('column',$this->column);



Dear ItsYII

You are absolutely correct.

And the solution is elegantly given by Reza M.




if(isset($this->ID))

            {

			if($this->weight==='0')

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

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

	    }



@Reza

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');

      else 

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

        }