Display multiple related data in yii2

enter image description hereWe create a control system using yii2. I try to display data from several to one, but it returns an error not set as shown

Tables:

Case table
Columns: ref_no (primary key),case_description,case_raised_on

Evidence table
Columns: ref_no(foreign key), path, evidence_type

A case may have several evidence, and each evidence belongs to one case.

RELATIONSHIPS IN THE MODELS:

Case Model:

 public function getEvidences()
{
    return $this->hasMany(Evidence::className(), ['case_ref' => 'ref_no']);
}



Evidence model
public function getEvidenceType()
{
    return $this->hasOne(EvidenceType::className(), ['type' => 'evidence_type']);
}

Controller

Case controller
public function actionView($id)
{
return $this->render('view', [
        'model' => $this->findModel($id),
]);
}

In the view file

<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'ref_no',
            'case_description',
            'raised_on',
            'status',
            'updated_on',
        'evidences.evidence_type',                           
        ],
    ]) ?>

The view file returns an error not set in (evidences.evidence_type). It should display a record of all evidence related to a particular case, referred to by ref_no2

+4
source share
2 answers

DetailView yii2 . , , :

[
'label' => 'Evidences',
'value' => implode(',',\yii\helpers\ArrayHelper::map($model->evidences, 'id', 'evidence_type')),
],

$model->evidences - , Case Model

+2

, Case . , ,

,

<?php
$evidences  = \yii\helpers\ArrayHelper::getColumn((\yii\helpers\ArrayHelper::getColumn($model , 'evidences')) ,'evidence_type');
?>
<?= \yii\widgets\DetailView::widget([
    'model' => $model,
    'attributes' => [
        'ref_no',
        'case_description',
        'raised_on',
        'status',
        'updated_on',
        [
            'label' => 'Evidences',
            'value' => implode(',' ,$evidences),
        ],
        'evidences.evidence_type',
    ],
]) ?>
+3

Source: https://habr.com/ru/post/1615119/


All Articles