I am having an issue with Yii relations. I am using CGridView to display a table in a view.
I have the following relations in my model:
'relationName' => array(self::BELONGS_TO, OtherModelName, link_id),
When I call the value in my CGridView like the example below it works fine:
'relationName.field_name',
When I try to call the value inside an array like the example below:
array('header'=>'tableHeaderName', 'value'=>'$data->relationName->field_name'),
My page fails and I get the following error: Trying to get property of non-object
Any ideas or suggestions? Thanks so much for your help.
I've run into this recently myself. You'll need something like this:
array(
'header'=>'tableHeaderName',
'value'=>'(isset($data->relationName)) ? $data->relationName->field_name : null',
)
What happens is that Yii freaks out if the relation isn't always there. So if you have any gaps in your data / relationships, then you run into problems.
Check if your relationName
has the same name with any of your DB table or not.
relationName
before rendering the GridView (usingwith
)? Are you sure that the related object is not null (is link_id not null) - DCoder 2012-04-05 19:26