Reputation: 780
I have a model/table solicitation with 2 fields (user_id and analyst_id) related to field id in model table user.
In my view, i have a gridview with fields:
[
'attribute' => 'user_id',
'enableSorting' => true,
'value' => function ($model) {
return $model->user->username;
},
]
How do same with analyst_id (i need show the username based another id) ?
UPDATE
Upvotes: 2
Views: 1094
Reputation: 33538
Simply create another relation for analyst:
Add in model:
public function getAnalyst()
{
return $this->hasOne(User::className(), ['id' => 'analyst_id']);
}
Displaying in GridView:
[
'attribute' => 'analyst_id',
'enableSorting' => true,
'value' => function ($model) {
return $model->analyst->username;
},
],
Or if the analyst might not exist replace return
part:
return $model->analyst ? $model->analyst->username : null;
Upvotes: 1
Reputation: 133360
I think
'value' => function ($model) {
return User::findOne($model->analyst_id)->username;
},
PS Add the correct namespace for User
Upvotes: 0