Hi guys,
I have two tables:
-
Main table:country
-
Lookup table:l_country
I just created two models, using Gii for both tables. Furthermore, I programmed manually following Controller,which works fine,so long:
<?php
/*
SELECT code,name,population,l_country.bez
FROM country
INNER JOIN l_country ON country.bez_id = l_country.id;
*/
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;
class CountryController extends Controller
{
public function actionIndex()
{
$model = Country::find();
//stellt fünf Records auf einmal dar und blättert dann weiter
$aufsplitten = new Pagination([
'defaultPageSize' => 5,
'totalCount' => $model->count()
]);
// stellt alle Records der Datenbank dar
/*
$query_join=$model->select(['country.code','country.name','country.population','l_country.bez'])
->from('country')
->leftJoin('l_country', 'country.bez_id = l_country.id')
->offset($aufsplitten->offset)
->limit($aufsplitten->limit)
->all();
*/
$query_join=$model->join('INNER JOIN', 'l_country', 'country.bez_id = l_country.id')->orderBy('name')
->offset($aufsplitten->offset)
->limit($aufsplitten->limit)
->all();
// ermittelt den maximalen Wert pro Reihe
$query_1 = $model->select('population')->max('population');
// ermittelt den durchschnittlichen Wert pro Reihe
$query_2=$model->select('population')->average('population');
// ermittelt den Gesamtwert pro Reihe
$query_3=$model->select('population')->sum('population');
return $this->render('index', [
'query_join' => $query_join,
'query_1'=>$query_1,
'query_2'=>$query_2,
'query_3'=>$query_3,
'aufsplitten' => $aufsplitten,
]);
}
}
?>
Following view only outputs Foreign Key(bez_id) of table Country.
But I want to get value(bez) of lookup table(l_country) instead blank Foreign Key as described in $query_join!
I included two Screenshots -
The first one outputs Yii2, the second one outputs phpmyadmin,as yii2 should also do,but it doesn’t!
<?php
use yii\widgets\LinkPager;
?>
<h2>Aggregate functions (per row)</h2>
<table>
<tr>
<th width="80">Function</th>
<th>Value</th>
</tr>
<tr>
<td>Maximum:</td>
<td><?php
$format= number_format($query_1, 2, ',', '.');
echo $format;?></td>
</tr>
<tr>
<td>Average:</td>
<td><?php
$format= number_format($query_2, 2, ',', '.');
echo $format;?></td>
</tr>
<tr>
<td>Summe:</td>
<td><?php
$format= number_format($query_3, 2, ',', '.');
echo $format;?></td>
</tr>
</table>
<h3>Countries</h3>
<ul>
<p> This output should show value according to foreignkey in lookup-Table,but it doesn't.What should I do?</p>
<?php
foreach ($query_join as $country){ ?>
<li>
<?php
echo"$country->name($country->code):$country->population,$country->bez_id" ?>
</li>
<?php } ?>
</ul>
<?= LinkPager::widget(['pagination' => $aufsplitten]) ?>