C
C
connor742015-10-29 19:46:36
Yii
connor74, 2015-10-29 19:46:36

How to implement a similar display of related fields?

There are 3 related tables and 3 models respectively:
Seminar (id, city_id, name, ...);
City (id, region_id, name);
Region (id, name).
In the form you need to display:
seminar.id - seminar.name - city.name - region.name The
city is specified in the Seminar model:

public function getCity()
{
        return $this->hasOne(City::className(), ['id' => 'city_id']);
}

Respectively in view output:
<?php foreach ($seminars as $seminar): ?>
<li>
    	<?=$seminar->date_begin;?>
    	<?=$seminar->city->name;?>
    	<?= Yii::$app->formatter->asDate($seminar->date_begin , 'php:d F');?>
        <?= Html::encode("{$seminar->title}") ?>
    </li>
<?php endforeach; ?>

How to display region.name here?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-10-29
@connor74

Add hasOne region id=>region_id to City and then $seminar->city->region->name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question