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.

Leave a reply to cialis Cancel reply

Blog at WordPress.com.