yii2中的视图不显示查找表中的外键值

时间:2017-05-26 20:40:58

标签: yii2

我有两个表,country和l_country.l_country包含值(bez),它涉及表country中的Foreign Key(bez_id)。 我编程了以下控制器,哪个工作正常........

<?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,
        ]);

    }
}
?>

不幸的是,以下视图没有显示查找表中的值。我收到错误“未知属性 - yii \ base \ UnknownPropertyException 获取未知属性: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]) ?>
如果我这样编程:

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

我没有得到任何错误,但此外,我没有得到l_country的价值(bez),而只是国家的外键(bez_id)。 任何帮助,请!!

1 个答案:

答案 0 :(得分:1)

似乎您的列名错误($ country-&gt; bex.id)

尝试使用正确的列名称

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

或聚合功能

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