Yii2 GridView column data as link to related table attribute
11,284
Try this..
[
'label' => 'Name',
'format' => 'raw',
'value' => function ($data) {
return Html::a($data->parentRegion->name, ['/admin/region/view', 'id' => $data->parent_id]);
},
],
Comments
-
kennySystemExit almost 2 years
I am trying to have
GridView
with one column in url link type. This one column is from relation in modelRegion
(asregion_type
- FK), specifically name attribute of this related table. In thisGridView
I got no problem to get value of name of these related table, but problem is to provide them as link format instead of plain name. I setparentRegion.name
as relation attribute in RegionSearch model.View:
<?= 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'], ], ]); ?>
RegionSearch:
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; }
Could you please help me how to get this property name (attribute of related table) to this
GridView
as link? -
kennySystemExit over 8 yearsNot working, I get this exception: Trying to get property of non-object
-
GAMITG over 8 yearslink appear in gridview ?
-
kennySystemExit over 8 yearsNo, I guess this is because $dataProvider has no property parentRegion. This property I can get from model class, but not from $dataProvider. Problem is to have this property in $dataProvider.
-
GAMITG over 8 yearsdo you have
parentRegion
relation inRegion
model ? -
kennySystemExit over 8 yearsIn my model Region I have public function getParentRegion() as I know this is I can acces by $model-> parentRegion as classic property
-
GAMITG over 8 years
ParentRegion
model have name property? -
Beowulfenator over 8 yearsIn this function
$data
refers to the model, not to the provider. Most likely only one of your models is missing it parentRegion. In other words, do something likeif (isset($data->parentRegion)) { return ...; } else { return null; }