Yii relations issue

Go To StackoverFlow.com

1

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.

2012-04-05 19:22
by bjtilley
Did you prefetch relationName before rendering the GridView (using with)? Are you sure that the related object is not null (is link_id not null) - DCoder 2012-04-05 19:26
If my answer helped, I'd appreciate an up vote, thx - acorncom 2012-04-09 13:01
Thanks acorncom that did the tric - bjtilley 2012-04-09 13:44
Great, glad that helped. Turns out I didn't mean an upvote, I meant "click the checkbox" :-) That means that your question has been answered - acorncom 2012-04-10 00:42


1

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.

2012-04-05 23:08
by acorncom
What would you expect Yii to do in this case to avoid the "freaking out" label? If you try to read from a null object, any programming language will "freak out" - DCoder 2012-04-06 12:42
Not a criticism of Yii, just an explanation of why he's seeing what he's seein - acorncom 2012-04-06 14:32


0

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

2012-04-06 12:36
by Sobit Akhmedov
Ads