Yii: Trying to get property of non-object

To the point, we’ve got this error notice right:

 

 

It comes from.., nah, this is my case, my erd is like this:

On Class relationships method  I define the model with the following code:

class TheClass extends CActiveRecord
{
....
public function relations(){
return array( 'faculty' => array(self::BELONGS_TO, 'Fakulty', 'faculty_id') );
}
.......
}

On Class view, i tried to get the name of faculty with this code:
echo CHtml::encode($data->faculty_id->name);
and I’d got the error notice. I’d been trying many solution and finally.. the problem is solved, the right code is:
echo CHtml::encode($data->faculty->name);
Remember to use the relationship variable name that we’ve defined on the class model, not the attribute name.

Hopefully this simple solution can fix your problem too..

Categories: IT | Tags: , , | 4 Comments

Post navigation

4 thoughts on “Yii: Trying to get property of non-object

  1. bhavesh

    thank you …….helped

  2. Great post.

Tinggalkan Jejakmu berupa komen

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: