[英]Get parent name of a user GridView Yii2
I want to get the parent username of a user我想获取用户的父用户名
The 'parent.username' works this way: 'parent.username' 是这样工作的:
But I need url the user profile, and I have it like this但我需要 url 用户配置文件,我有这样的
[
'attribute' => 'parent_id',
'value' => function(User $model) {
return Html::a(Html::encode($model->parent_id ? $model->parent_id : " "), ['view', 'id' => $model->parent_id]);
},
'format' => 'raw',
]
It is possible to get the username?可以获取用户名吗?
Thank you谢谢
In the User class:在用户 class 中:
public function getParent(){
return $this->hasOne(User::class,['id'=>'parent_id']);
}
In the gridView:在 gridView 中:
[
'attribute' => 'parent.id', // or parent.first_name (whatever is relevant to you)
'value' => function($model) {
if(isset($model->parent->id)) // or parent->first_name (whatever is relevant to you)
return Html::a(Html::encode($model->parent->id),
['view', 'id' => $model->parent->id]);
return "not found";
},
'format' => 'html',
]
In the User class:在用户 class 中:
public function getParent(){
return $this->hasOne(User::class,['id'=>'parent_id']);
}
In the gridView:在 gridView 中:
[
'attribute' => 'parent_id',
'label' => 'User Name',
'format' => 'raw',
'value' => function ($dataProvider) {
$name = Html::a($dataProvider->parent->username,['view','id'=>$dataProvider->parent_id]);
return $name;
},
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.