2つのテーブルがあり、l_country.l_countryにはテーブルの外部キー(bez_id)に関する値(bez)が含まれています。 は私が 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,
]);
}
}
?>
........正常に動作し、次のコントローラーを、プログラム残念ながら、ビューを以下のことは私にルックアップテーブルの値を表示しません。私はこのようなプログラムの開発場合
<?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" ?>
私は - 私はエラー "のアプリ\モデル\国:: BEZ未知のプロパティを取得するのYii \ベースの\ UnknownPropertyException 不明なプロパティ" を取得しますエラーは発生しませんが、さらに、私はl_countryの値(bez)を取得するのではなく、単に国のForeign Key(bez_id)を取得します。 助けてください、よろしくお願いします!
のための適切なカラム名を使用してみてください、あなたが間違った列名を持っているようです確かに '$ query_join'部分の' - > select() 'オプションで試してみましたか? [関係を結ぶ](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#joining-with-relations) – leninhasda
l_countryテーブルのモデルを(giiで)作成し、 Countryに$ this-> hasOne()を使ってリレーションを処理する関数を追加します。 [リレーショナルデータを扱う](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data) – sonofagun