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



我有两个表,国家和l_country.l_country包含值(bez(,它涉及表国家中的外键(bez_id(。我按照控制器编程,它工作正常......

<?php
/*
SELECT code,name,population,l_country.bez
FROM country
INNER JOIN l_country ON country.bez_id = l_country.id;
*/
namespace appcontrollers;
use yiiwebController;
use yiidataPagination;
use appmodelsCountry;
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\未知属性异常获取未知属性:应用\模型\国家::bez">

<?php
use yiiwidgetsLinkPager;
?>
<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(。任何帮助,请!!

似乎您的列名错误($country->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');

最新更新