[英]Get parent name of a user GridView Yii2
我想獲取用戶的父用戶名
'parent.username' 是這樣工作的:
但我需要 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',
]
可以獲取用戶名嗎?
謝謝
在用戶 class 中:
public function getParent(){
return $this->hasOne(User::class,['id'=>'parent_id']);
}
在 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',
]
在用戶 class 中:
public function getParent(){
return $this->hasOne(User::class,['id'=>'parent_id']);
}
在 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.