Hi, newbie on Yii. Basically, trying to figure out how to display the related field on another table.
Model:
- Users (relations)
'recipients' => array(self::HAS_MANY, 'Recipients', 'user_id'),
- Recipients (relations)
'users' => array(self::BELONGS_TO, 'Users', 'id'),
Controller (Action: Recipients/index):
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Recipients', array(
'criteria' => array(
'with' => array('users')
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
View:
<?php
echo CHtml::encode($data->user_id); // Default, works!
echo CHtml::encode($data->users->getAttribute('name')); // Works, can retrieve users->name but template breaks
print_r($data->users->getAttributes()); // Works, can retrieve array of users but template breaks
echo $data->users->username; // Error, Trying to get property of non-object
echo $data->users->id; // Error, Trying to get property of non-object
?>
Not sure what I missed, but I can see that CActiveDataProvider was able to relate to the users table. But, when retrieve on the view using $data->users->username, simply throws an invalid get property of non-object which is weird.
Any help is appreciated!
Thanks