I am trying to have GridView
with one column in url link type. This one column is from relation in model Region
(as region_type
- FK), specifically name attribute of this related table. In this GridView
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 set parentRegion.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?