tklustig
tklustig

Reputation: 503

Views in yii2 doesn't show Foreign key value in lookup table

I have two tables,country and l_country.l_country contains value(bez),which concerns Foreign Key (bez_id) in table country. I programmed following Controller,which works fine........

<?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->join('LEFT 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,
        ]);

    }
}
?>

Unfortunately,following view doesn't show me value in lookup-table. I get error"Unknown Property – yii\base\UnknownPropertyException Getting unknown property: app\models\Country::bez"

<?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" ?>
    </li>
<?php } ?>
</ul>

<?= LinkPager::widget(['pagination' => $aufsplitten]) ?>
If I programm like this:

        echo"$country->name($country->code):$country->population,$country->bez.id" ?>

I get no error,but furthermore, I don't get value(bez) of l_country, but merely Foreign Key(bez_id) of country. Any help,PLEASE!!

Upvotes: 1

Views: 294

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

seems you have wrong column name ($country->bex.id)

try using the proper column name

   echo"$country->name($country->code):$country->population,$country->bez_id" ?>

or for aggregatio function

   $query_1   = Country::find()      
      ->innerJoin('l_country', '`l_country`.`id` = `country`.`bez_id`')->max('population');

Upvotes: 1

Related Questions