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]);
                     },
    ],
Share:
11,284
kennySystemExit
Author by

kennySystemExit

I simply love programming

Updated on June 27, 2022

Comments

  • kennySystemExit
    kennySystemExit almost 2 years

    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?

  • kennySystemExit
    kennySystemExit over 8 years
    Not working, I get this exception: Trying to get property of non-object
  • GAMITG
    GAMITG over 8 years
    link appear in gridview ?
  • kennySystemExit
    kennySystemExit over 8 years
    No, 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
    GAMITG over 8 years
    do you have parentRegion relation in Region model ?
  • kennySystemExit
    kennySystemExit over 8 years
    In my model Region I have public function getParentRegion() as I know this is I can acces by $model-> parentRegion as classic property
  • GAMITG
    GAMITG over 8 years
    ParentRegion model have name property?
  • Beowulfenator
    Beowulfenator over 8 years
    In 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 like if (isset($data->parentRegion)) { return ...; } else { return null; }