S
S
Sergey Beloventsev2016-11-14 20:17:06
Yii
Sergey Beloventsev, 2016-11-14 20:17:06

How to display two models in Gridview?

There are two models Goodsand AddFeild. Addfeildlinked to Goodsthe field id_goodby link hasManythere is a need to output through Gridviewthe content of the model Goods, and all the data of the associated model Addfeild. Please don't tell me how this can be done. Or where to find how it is implemented.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-11-14
@Sergalas

Create a link in the model:

public function getAddFeild(){
return $this->hasOne(AddFeild::className(),['id_good'=>'id']);
}

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

            'id',
            'title',
            'addFeild.someAttribute',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question