如何在 gridview 和 detailview yii2 中检索其他参数而不是 id?
How to retrieve other parameter instead of id in gridview and detailview yii2?
在角色、职位、部门列中,您可以看到这些数据库表的 ID "role"、"position"、"department"。
如何粘贴名称而不是 ID?
提前致谢。
使用匿名函数改变属性值。而不是简单的
role_id,
在 GridView 列列表中使用数组
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role_id;
},
],
现在这将为您提供完全相同的结果,但您可以对其进行自定义。例如:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
switch ($model->role_id) {
case 1:
return 'User';
case 2:
return 'Moderator';
case 3:
return 'Admin';
}
},
],
也许您已经创建了方法,其中包含在您的用户中命名的所有角色 class:
public static function listRoles()
{
return [
1 => 'User',
2 => 'Moderator',
3 => 'Admin',
];
}
因此您可以在 GridView 中使用它,例如:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return User::listRoles()[$model->role_id];
},
],
如果是关系数据,您也可以使用它:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
],
$model
是当前显示的 User 实例。
阅读 Guide 中有关 GridView 列的更多信息。
对于详细视图检查模型中的关系并使用您这样做的关系:-
'attributes' => [
'title',
'relational_data',
[
'label' => 'Field Name',
'value' => $data->relation_table->field_name,
],
],
对于网格视图:-
[
'attribute' => 'role_id',
'value' => function ($data) {
return $data->relation_table->field_name;
},
],
你的Table关系应该是这样的:-
public function getRelation_table()
{
return $this->hasOne(Table::className(), ['relation_id' => 'id']);
}
如果是关系数据,我会添加到@Bizley 解决方案中:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
],
不要忘记预先加载,你应该将 UsersSearch 添加到 search 函数中
$query = Users::find()->with('role');// eager loading
而不是
$query = Users::find();// lazy loading
并使用角色名称进行过滤添加
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
'filter'=>ArrayHelper::map(\app\models\Role::find()
->asArray()- >all(), 'id', 'name')
],
这将在过滤中添加一个组合框,其中填充了所有角色名称,如果您想要输入文本过滤选项,还有另一种方法可以做到:
去掉上面添加的过滤选项:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
],
使 joinWith 而不是 with
$query = Users::find()->joinWith('role');
- 在 UsersSearch rules 中将角色从整数更改为安全
- 在 search 函数中删除
'role' => $this->role,
并替换为
->andFilterWhere(['like', 'roles.name', $this->role])
在角色、职位、部门列中,您可以看到这些数据库表的 ID "role"、"position"、"department"。
如何粘贴名称而不是 ID?
提前致谢。
使用匿名函数改变属性值。而不是简单的
role_id,
在 GridView 列列表中使用数组
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role_id;
},
],
现在这将为您提供完全相同的结果,但您可以对其进行自定义。例如:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
switch ($model->role_id) {
case 1:
return 'User';
case 2:
return 'Moderator';
case 3:
return 'Admin';
}
},
],
也许您已经创建了方法,其中包含在您的用户中命名的所有角色 class:
public static function listRoles()
{
return [
1 => 'User',
2 => 'Moderator',
3 => 'Admin',
];
}
因此您可以在 GridView 中使用它,例如:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return User::listRoles()[$model->role_id];
},
],
如果是关系数据,您也可以使用它:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
],
$model
是当前显示的 User 实例。
阅读 Guide 中有关 GridView 列的更多信息。
对于详细视图检查模型中的关系并使用您这样做的关系:-
'attributes' => [
'title',
'relational_data',
[
'label' => 'Field Name',
'value' => $data->relation_table->field_name,
],
],
对于网格视图:-
[
'attribute' => 'role_id',
'value' => function ($data) {
return $data->relation_table->field_name;
},
],
你的Table关系应该是这样的:-
public function getRelation_table()
{
return $this->hasOne(Table::className(), ['relation_id' => 'id']);
}
如果是关系数据,我会添加到@Bizley 解决方案中:
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
],
不要忘记预先加载,你应该将 UsersSearch 添加到 search 函数中
$query = Users::find()->with('role');// eager loading
而不是
$query = Users::find();// lazy loading
并使用角色名称进行过滤添加
[
'attribute' => 'role_id',
'value' => function ($model, $key, $index, $column) {
return $model->role->name;
},
'filter'=>ArrayHelper::map(\app\models\Role::find()
->asArray()- >all(), 'id', 'name')
],
这将在过滤中添加一个组合框,其中填充了所有角色名称,如果您想要输入文本过滤选项,还有另一种方法可以做到:
去掉上面添加的过滤选项:
[ 'attribute' => 'role_id', 'value' => function ($model, $key, $index, $column) { return $model->role->name; }, ],
使 joinWith 而不是 with
$query = Users::find()->joinWith('role');
- 在 UsersSearch rules 中将角色从整数更改为安全
- 在 search 函数中删除
'role' => $this->role,
并替换为
->andFilterWhere(['like', 'roles.name', $this->role])