gpt4 book ai didi

php - Yii2 GridView 列数据作为相关表属性的链接

转载 作者:行者123 更新时间:2023-12-04 02:19:25 24 4
gpt4 key购买 nike

我正在尝试让 GridView 在 url 链接类型中包含一列。这一列来自模型 Region 中的关系(如 region_type - FK),特别是此相关表的名称属性。在此 GridView 中,我可以毫无问题地获取这些相关表的名称值,但问题是将它们提供为链接格式而不是纯名称。我将 parentRegion.name 设置为 RegionSearch 模型中的关系属性。

查看:

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],

[
'label' => 'View parent',
'format' => 'raw',
// here comes the problem - instead of parent_region I need to have parent
'value' => function ($dataProvider) {
return Html::a('view', '/admin/region/view?id=' . $dataProvider->parent_region);
},
],

'parentRegion.name', // this is what I want to display in link value - name of this related data


['class' => 'yii\grid\ActionColumn'],
],
]); ?>

区域搜索:

public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['parentRegion', 'regionType']);
}

public function search($params)
{
$query = Region::find();

$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

$query->joinWith(['parentRegion' => function($query) { $query->from(['parentRegion' => 'region']); }]);
$dataProvider->sort->attributes['parentRegion'] = [
'asc' => ['parentRegion' => SORT_ASC],
'desc' => ['parentRegion' => SORT_DESC],
];

$this->load($params);

if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}

$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'parentRegion', $this->getAttribute('parentRegion')])

return $dataProvider;
}

你能帮我把这个属性名(相关表的属性)作为链接获取到这个 GridView 吗?

最佳答案

试试这个..

  [
'label' => 'Name',
'format' => 'raw',
'value' => function ($data) {
return Html::a($data->parentRegion->name, ['/admin/region/view', 'id' => $data->parent_id]);
},
],

关于php - Yii2 GridView 列数据作为相关表属性的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com