yii2 - User role in gridview -


how print user role in yii 2 gridview widget? i've tried code:

public function getuserrole($id) { $roles = \yii::$app->authmanager->getrolesbyuser($id);

    $role = '';      foreach($roles $key => $value)     {         $role = $key;     }      return $this->role = $role; } 

in user model class it's not working. each user has assigned 1 role. many help.

ok, got it!

view:

<?= gridview::widget([     'dataprovider' => $dataprovider,     'filtermodel' => $searchmodel,     'columns' => [         ['class' => 'yii\grid\serialcolumn'],         //'id',         [         'label' => 'nazwa użytkownika',         'value' => 'username'         ],         //'auth_key',         //'password_hash',         //'password_reset_token',         'email:email',         [             'label' => 'rola',             'value' => 'userrole',         ],         // 'status',         // 'created_at',         // 'updated_at',          ['class' => 'yii\grid\actioncolumn'],     ], ]); ?> 

method in user model class:

public function getuserrole() {     $roles = \yii::$app->authmanager->getrolesbyuser($this->id);      $role = '';      foreach($roles $key => $value)     {         $role = $key;     }      return $role; } 

and in usersearch class attribute:

public $userrole; 

and

[['userrole'], 'safe']  

in rules usersearch


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -