Hello,
I’m currently working on a GridView in Yii2 where one column in particular is a relation of “has many”. Currently i’m showing the column information using the following code in the GridView widget:
'columns' => [
'email:email',
'username',
'signup_time:datetime',
[
'header' => Yii::t('app','Role(s)'),
'content' => function ($model, $key, $index, $column){
$roles = $model->getRoles()->all();
$string = "";
if(count($roles)>0){
if(count($roles)>1){
$string .="| ";
foreach ($roles as $role) {
$string .= $role->getAttribute('name')." | ";
}
}else{
$string .= $roles[0]->getAttribute('name');
}
}
return $string;
}
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
],
],
on the model side the relation is defined as follows:
/**
* @return \yii\db\ActiveQuery
*/
public function getRoles()
{
return $this->hasMany(TcWebRole::className(), ['id' => 'role_id'])->viaTable('user_role', ['user_id' => 'id']);
}
this resulting GridView is as shown in the image below
7385
What i need to do is make the column ‘Role(s)’ searchable and sortable like the other fields in the GridView. I assume this needs to be done via the search model but not sure how to go about doing that. Would appreciate any suggestions on how i should proceed.
thanks in advance