A
A
Artyom2015-05-15 21:29:24
Yii
Artyom, 2015-05-15 21:29:24

How to get full name in gridview in yii2?

Tell me, here's how to get the full name by ID from another table and display it in the gridview?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Artamonov, 2015-05-15
@DeOne

If I understand you correctly, then something like this:
Let's say you have Books and Authors models. And you want to display a list of books with the name of the author in the GridView.

<?= GridView::widget([
                'dataProvider' => $dataProvider,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],
                    'title',
                    'author' => [
                        'class' => \yii\grid\DataColumn::className(),
                        'format' => 'html',
                        'value' => function ($model, $index, $widget) {
                            return $model->author->name ;
                        },
                    ],
                   // и т.п. ...

And to access $model->author->... you must set up a relationship between models. In our example, in the Books model, we need to add something like this:
public function getAuthor() {
        return $this->hasOne(Authors::className(), ['id' => 'author_id']);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question