Issue With Cgridview And Error Handling On A Non-Required Relational Field

Hi folks,

I am hoping there is a solution to this.

I am currently trying to customize the admin section of one of my models to display the relational value and also to be able to search with it.

Currently, CGridView has this in it: -


array( 'name' => 'person_search', 'value' => '$data->person->person_name' ),

This is all well and good but the relation is not required and I get the error “Trying to get property of non-object” when there is no relational set. This is obviously understandable from a technical point of view, but I would like this error to be handle in a way such as it somehow checks for the existence of the object. If it exists, it displays the relational value. If it doesn’t, it displays nothing (as there is nothing in the relationship. Dealing with this in a normal code routine is easy enough but I am pretty new to CGridView and I don’t know how to deal with this. BTW, the search function itself is fine if there is a relation to search with.

I am not too sure, but I think the only way to sort this might be to go back to the model creation in the controller and add in the missing object if it not found in the newly created model?

Any thoughts?

Just exchange


array( 'name' => 'person_search', 'value' => '$data->person->person_name' ),

with


'person.person_name',

Thanks, but I am not getting the error “Use of undefined constant person - assumed ‘person’”.

Any ideas?

Are you sure that you exchanged the code how I said?

I guess you did it like that:


array( 'name' => 'person_search', 'value' => '$data->person->person_name' ),

to


array( 'name' => 'person_search', 'value' => 'person.person_name' ),

which is wrong.

Yep, you were right and your solution worked except for now it has taken away the corresponding search box?

Hi U4EA and Coksnuss,

I guess that U4EA has read the great wiki written by redguy.

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

"person_search" is a virtual attribute for searching.

Although "person.person_name" is enough for displaying the related value, you need to use a native attribute of the main model when you want to search by it.

So, this looks quite right. It should work perfect for BELONGS_TO relation with FK constraint.

But if the relation is HAS_ONE (or if the BELONGS_TO relation doesn’t have the constraint), “person” could be NULL and you have to check the existence of the relation manually.

A quick solution could be something like this:




array( 'name' => 'person_search', 'value' => '$data->person ? $data->person->person_name : ""' ),



Or you may create a getter function in the model.




public function getPersonName()

{

    return $this->person ? $this->person->person_name : "";

}

...

array( 'name' => 'person_search', 'value' => '$data->personName' ),



softark,

Thank you very much for that post. I shutdown NetBeans and localhost about 20 mins ago and hopped into bed but I just read that and wanted to say thanks as it looks very much like the solution I was after. I will try it out first thing tomorrow and get back to you.

That’s working perfectly softark, thank you.

My problem was never with the logic to handle the possible error but I see now that the PHP statement is just entered as a string then later executed as PHP code itself in the model?

Coksnuss,

Thanks again for your help.

Yes, the expression is evaled.

http://php.net/manual/en/function.eval.php