Search By Related Model Name In Cgridview

I looked all over the place for an answer to this and found several articles, but no answer to my question.

I wanted to display (and search by) a related model’s name column instead of by the current model’s id.

Found this excellent article: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/ and couldn’t get it to work.

Turns out it was down to my db design - in the main model I have a column named grouping, related to another model’s id. Since I already had a variable setup in the model called grouping, the relation somehow broke and it kept giving me a trying to get property of a non-object error. Instead of the grouping var referring to the relation table it was referring to its own grouping column. I fixed it by changing the name of the column to group_id. Just wanted to put this out in case someone else has the same problem.

As a side note, I went crazy for about 20 minutes getting a undefined variable: data error from CGridView.

CGridView $data items need to be in parenthesis:

array( ‘name’=>‘deadline’, ‘value’=>’$data->deadline->name’)

I was trying:

array( ‘name’=>‘deadline’, ‘value’=>$data->deadline->name)