2017-05-26 50 views
1

我有两个表,国家和l_country.l_country包含值(bez),它涉及表国家中的外键(bez_id)。 我编程的跟随控制器,它工作得很好........ yii2中的视图不会在查找表中显示外键值

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

 
    } 
 
} 
 
?>

不幸的是,以下视图不显示我在查找表值。我得到错误 “财产来源不明 - 警予\基地\ UnknownPropertyException 获得未知属性:应用程序\型号\国家:: 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]) ?>
如果我PROGRAMM这样的:

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

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

+0

我不是完全肯定,但你有没有尝试' - > select()'选项'$ query_join'部分? [加入关系](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#joining-with-relations) – leninhasda

+0

为您的l_country表创建一个模型(使用gii),并且向Country添加一个函数来处理使用$ this-> hasOne()的关系。请参阅[使用关系数据](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data) – sonofagun

回答

1

看来你有错误的列名($国家 - > bex.id)

尝试使用适当的列名

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

或aggregatio功能

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

我已发布,$ country-> bez_id只是显示Foregn重点,但不可查找的价值! – tklustig

+0

答案更新了聚合功能 – scaisEdge