Hello guys,
I have a problem with extracting data from a joined table using CActiveDataProvider and I couldn’t find the fix for it even after hours of internet search, so finally I am here asking for help.
The scenario goes this way:
My page have to display all the existing users(User table) along with the friendship invitations(Invitation table) sent by me to each user.
So the relations for UserModel is here:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'invitation' => array(self::HAS_MANY, 'Invitation', 'friend_id'),
);
}
Up above in the relations function I try to relate Invitation table to user that got an invitation sent by me.
In the Controller I have:
$criteria = new CDbCriteria();
$criteria->select = '*, invitation.id as invitid';
$criteria->with = array(
'invitation' =>array(
'joinType' => 'LEFT JOIN',
'on' => 'invitation.id_user = :userId',
'params' => array(':userId' => Yii::app()->user->id),
)
);
$users = new CActiveDataProvider('Users', array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 15,
),
));
In my View I have :
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $users,
'itemView' => '_usersView',
'template' => '{items} {pager}',
));
Now I get all the users along with the Invitation columns, but my problem is that I get no data for Invitation Columns. When I try to access the invitation id($data->invitation->id) I get nothing and my database has this information:
My account information from User table:
id: 80
fullname: adrian holobut
email: adrian.holobut@gmail.com
data_created: 2013-01-23 13:20:33
My friend information row from User table
id: 1
fullname: Andrei Nartea
email: duca14036@gmail.com
data_created: 2011-10-11 00:00:00
Invitation table:
id: 40
user_id: 80
friend_id: 1
data: 2013-01-24 07:38:01
code: a6dcea5f6fa917ebce77519aed193b50
So I expect to get along with my friend information, the invitation table row from above.
Thanks,
Adrian.