[solved] CGridView + empty foreign key

I have a CGridView and I am trying to display the name of a record thru a foreign key. However that foreign key may not always be populated. So if I do $data->foreignKeyId->name I get the error "Trying to get property of non-object" because no object was found because the key was empty, which is not an error in itself.

How do I check if the foreign key is not null before trying to get the value of the associated record?

The syntax I am using is:






You probably should use the ternary operator in the expression.

condition ? value if true : value if false


Thanks, I wasn’t sure that would work and was hung up on trying to get evaluateExpression to work first. I finally got it to work with:



    'value'=>'CValidator::isEmpty($data->foreignKeyId) ? "" : $data->foreignKey->name',