M
M
MaikMain2018-02-23 18:14:33
Yii
MaikMain, 2018-02-23 18:14:33

DetailView and GridView. Displaying the name of the line in related tables. Yii2?

Please tell me, otherwise I broke my whole head.
I have 2 tables: one with courses (course) and the other with authors (author).
I'm in the "Course" in view () - in the form I display a list of course authors:

<?php $authors = \common\models\Author::find()->where(['status' => 1])->all();
     $items = \yii\helpers\ArrayHelper::map($authors,'id','name');
     $params = ['prompt' => '--- Укажите автора ---'];
     echo $form->field($model, 'author',['options' => ['class' => 'col-xs-2']])->dropDownList($items,$params);
?>

When saving, the author's id is written in the "author" field of the "course" table. In principle, it is necessary. But when I display it in DetailView and GridView it is not displayed and writes "(not set)":
<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            [
                'attribute' => 'category_id',
                'value' => function($data){
                    return $data->category->name;
                },
            ],
            'name',
            'content:raw',
            [
                'attribute' => 'author',
                'value' => function($model){
                    return $model->author->name;
                },
            ],
            'description',
            [
                'attribute' => 'statusName',
                'value' => function($model) {
                     return  Html::tag('span',
                                $model->getStatusName(),
                                ['class' => 'label label-' . ArrayHelper::getValue([0 => 'danger', 1 => 'success'], $model->status_id)]
                            );
                    },
                'format' => 'raw',
            ],
            'smallImage:image',
        ],
    ]) ?>

and
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'class' => 'yii\grid\ActionColumn',
            'header' => 'Действия',
            'options' => ['width' => '10']
        ],
        [
            'attribute' => 'category_id',
            'value' => function ($data) {
                return $data->category->name;
            },
            'options' => ['width' => '200']
        ],
        'name',
        [
            'attribute' => 'author',
            'value' => function ($data) {
                return $data->author->name;
            },
            'options' => ['width' => '200']
        ],
        'smallImage:image',
        [
            'attribute' => 'status_id',
            'filter' => \common\models\Course::getStatusList(),
            'value' => 'statusName',
        ],
    ],
]); ?>

I connected the tables like this:
1. Author:
public function getCourse()
    {
        return $this->hasMany(Course::className(), ['author' => 'id']);
    }

2.Course:
public function getAuthor()
    {
        return $this->hasOne(Author::className(), ['id' => 'author']);
    }

Please tell me what could be the problem? Thank you very much in advance))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-02-23
@MaikMain

Good evening.
These two lines must be moved to a separate method.

$authors = \common\models\Author::find()->where(['status' => 1])->all();
     $items = \yii\helpers\ArrayHelper::map($authors,'id','name');

Show the database query you use for GridView and DetailView. Are you linking there?
It should be something like this:
$query = Author::find()->with('course');
// далее подставляете это в ActiveDataProdiver

ps The value is not quite right. You are passing a parameter to a function.
'value' => function ($model) {
                return $model->author->name;
            },

But you can write like that.
'value' => 'author.name'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question