[Solved] Handle NULL value in CgridView

Hello,

In brief, I have a table Tip, which has a FK pointing to TipAction.id The relation is correctly setup by Gii. However, the FK column in Tip can be set to NULL as the Action field is not compulsory.


array(

    'name' => 'type_id',

    'value' => '$data->type->name',

    'filter' => CHtml::listData(TipType::model()->findAll(), 'id', 'name'),

),

array(

    'name' => 'action_id',

    'value' => '$data->action->name',

    'filter' => CHtml::listData(TipAction::model()->findAll(), 'id', 'name'),

),

The first column works correctly (it is not NULLable). For the second column, I get PHP error:


Trying to get property of non-object

I understand that erroris because of NULL value.

So my query is - for a FK column that may have NULL values, how do I get them to display in CGridView with filters?

Check if action is null like


'value'=>'$data->action==null ? '' : $data->action->name',

That worked correctly! (with double quotes after the ? sign) I had tried something like that - only the comparison part and the operators were outside the quotes, which resulted in error.

However, I am going to enforce the required condition and create an additional entry in the Action table for this situation. The reason - I cannot filter display the NULL ones only as passing an empty value to search() clears out the filters.

Thanks a lot!

Yes, the double quotes are needed there (typing error)…

For the filtering…

you can always use here some special symbol instead of ""… or for example "null" or "empty"

and for the filtering to work… when the user enter that text you are showing in the table as null… in the $model->search() method just check if the value is equal to that text… and in that case add a search criteria for null value instead of the entered text…

Hi, took a week to come back to this - was rushed with the project ;D In that particular case, I rammed through the idea of compulsorily selecting an action type.

This idea is useful in other cases where I do have null values; which I am going to encounter soon.

So far, picking from Yii’s own strings, ‘Not set’ corresponds to null. In model’s search(),


$criteria->compare('c_key',if ($this->c_key == 'Not Set') ? null : $this->c_key ,true);

Of course, I need to have this populated in the dropdown filter too - I will have to use some of CHtml’s functions to add “Not Set” in the filter values along with the Action model’s values. I will cross that bridge when I come to it.

I also realised that if you have a relation you need to test for the relation first instead of relation value

‘value’=>’$data->Relation == null?"":$data->Relation->value’

istead of

‘value’=>’$data->Relation->value == null?"":$data->Relation->value’

In addition if you want to check for null (and not empty string) in sql database you could use this one:




if ($this->c_key != 'Not Set') {

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

} else {

     $criteria->addCondition('c_key IS NULL');

}



Thanks Kostas, that’s just what I needed!

So, putting this together with what’s been said above, if you have a related table that might have be null, and you want be able to filter on ALL values or on the ‘Not Set’ ones you can use something like:




  'value'=>'$data->Relation == null?"":$data->Relation->c_key' 

  'filter'=> array(1=>'Item1', 2=>'Item2',..., 'Not Set'),



…or change ‘Not Set’ to another key value