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?
"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.
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.
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?