bjtilley
bjtilley

Reputation: 1970

Yii relations issue

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.

Upvotes: 1

Views: 1203

Answers (2)

Sobit Akhmedov
Sobit Akhmedov

Reputation: 392

Check if your relationName has the same name with any of your DB table or not.

Upvotes: 0

acorncom
acorncom

Reputation: 5955

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.

Upvotes: 1

Related Questions