Greetings,
Before i state my question, I would liek to clarify, I have searched the forum, and found some post regarding this issue. I followed it as close as i can, however I am still left with the following issue:
I have 2 tables:
User - with the folllowing relationship defined in the model: ‘role’ => array(self::BELONGS_TO, ‘Role’, ‘id’)
and "Role" with no relation defined(thats what i understand from how BELONGS_TO works, as i read in another thread)
The user table has a column named role_id
The role table has an id and a name
in the gridview, i want a column to show the name of the role that belongs to a user.
This is what I currently have:
Model->search():
$criteria=new CDbCriteria;
$criteria->compare('t.id',$this->id);
$criteria->compare('role.name',$this->role, true);
$criteria->compare('t.username',$this->username,true);
$criteria->compare('t.password',$this->password,true);
$criteria->compare('t.salutation',$this->salutation,true);
$criteria->compare('t.first_name',$this->first_name,true);
$criteria->compare('t.last_name',$this->last_name,true);
//load the related table at the same time:
$criteria->with=array('role');
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
in my controller i have:
$oUser = new User('search');
$oUser->unsetAttributes();
$this->render('index', array(
'oUser' => $oUser
));
and last, the view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$oUser->search(),
'columns'=>array(
'username',
'salutation',
'first_name',
'last_name',
array(
'name'=>'role',
'value'=>'$data->role->name',
),
array(
'class'=>'CButtonColumn',
),
),
));
The current error I get is trying to get property of non-object.
Altough the error is clear, I still find myself failing at finding the solution…
Anyone that could tell me what im doing wrong?
Thanks for taking the trouble of reading this