D
D
dwpoint2022-04-21 20:04:16
Yii
dwpoint, 2022-04-21 20:04:16

How to link two tables in DetailView?

Friends, tell me, please. I have 2 tables in the database. Let the first be a product, and the second category. The table with products has category ids. If you display it in the DetailView, then of course, it will be just an id that does not tell a person anything. Can you please tell me how to make this id check with the category table and display the name?

View code:

<?php

use core\forms\faq\FaqCategoryItemForm;
use core\forms\faq\FaqItemForm;
use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model FaqItemForm */

$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Ответ-Вопрос', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\backend\helpers\TemplateHelper::boxWrap($this->params);
?>
<div>
    <p>
        <?= Html::a('Редактирование', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Удалить', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger']) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'name',
            'description',
            [
                'attribute' => 'category_id',
                'format' => 'html',
                'value' => function($model) {
                    return '';
                },
            ],
        ],
    ]) ?>

</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dwpoint, 2022-04-21
@dwpoint

In general, by typing, I somehow did it somehow.
Here is the code in the view:

<?php

use core\entities\Faq\FaqItem;
use core\forms\faq\FaqItemForm;
use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model FaqItemForm */

$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Ответ-Вопрос', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\backend\helpers\TemplateHelper::boxWrap($this->params);
?>
<div>
    <p>
        <?= Html::a('Редактирование', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Удалить', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger']) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'name',
            'description',
            [
                'attribute' => 'category_id',
                'format' => 'html',
                'value' => function(FaqItem $model) {
                    return $model->getName();

                },
            ],
        ],
    ]) ?>

</div>

And be sure to write two methods in core\entities\Faq\FaqItem:
public function getNameCategory()
    {
        return $this->hasOne(FaqCategory::className(), ['id' => 'category_id']);
    }

    public function getName() {
        return $this->nameCategory->name;
    }

The place in the view where I worked:
[
                'attribute' => 'category_id',
                'format' => 'html',
                'value' => function(FaqItem $model) {
                    return $model->getName();

                },
            ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question