2016-04-15 17 views
0

joinWith()メソッドを使用して2つのテーブルからいくつかのフィールドを選択する必要があります。私は2つのテーブルを持っています:アパートと都市。joinWith null null外部キー値

アパートテーブルはcityIdフィールドを持ち、の表は、2つのフィールドがあります:idと値を。このような

何か:

enter image description here

私は アパートテーブルからいくつかのフィールドを選択したい

経由cityIdフィールドは都市の名前()を取得します。

アパートモデルにおける関係:市内の

public function getCity() { 
    return $this->hasOne(City::className(), ['id' => 'cityId']); 
} 

関係モデル:コントローラで

public function getApartments() { 
    return $this->hasMany(Apartments::className(), ['cityId' => 'id']); 
} 

私はjoinWith()メソッドを使用してクエリを構築し、それがcity.valueを返しませんフィールド。

$apartments = Apartments::find()->select('apartments.title, city.value')->joinWith('city')->all(); 

print_r($apartments)戻り、この:私はこのレスポンスにcity.valueフィールドを参照してくださいが、任意の値なしフィールドが表示されていない

Array ([0] => app\models\Apartments Object ([file] => [_attributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 1) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 1) [_related:yii\db\BaseActiveRecord:private] => Array ([city] =>) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ()) [1] => app\models\Apartments Object ([file] => [_attributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 2) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 2) [_related:yii\db\BaseActiveRecord:private] => Array ([city] =>) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ()) [2] => app\models\Apartments Object ([file] => [_attributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 3) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 3) [_related:yii\db\BaseActiveRecord:private] => Array ([city] =>) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ()) [3] => app\models\Apartments Object ([file] => [_attributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 4) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 4) [_related:yii\db\BaseActiveRecord:private] => Array ([city] =>) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ()) [4] => app\models\Apartments Object ([file] => [_attributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 5) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 5) [_related:yii\db\BaseActiveRecord:private] => Array ([city] =>) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ()) [5] => app\models\Apartments Object ([file] => [_attributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 6) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([title] => Квартира 6) [_related:yii\db\BaseActiveRecord:private] => Array ([city] =>) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ())) 

私を助けることができますか?問題は何ですか?

答えて

1

をcity.id結合された値 これはあなたが何を意味するのかではない場合

$apartments = Apartments::find()->all(); 
foreach($apartments as $apartment) 
{ 
    echo $apartment->city->value; 
} 

は申し訳ありませんが、このコードを試してみてください!

+0

ああ、ありがとう。 – rmpstmp

+0

喜んで:) – Midhun

2

これを試してみてください:

$query = new Query; 
$query->select(['apartments.title as app_title', 'city.value as city_val']) 
       ->from('apartments') 
       ->join('INNER JOIN', 
        'city', 
        'city.id =apartments.cityId' 
       ); 

    $command = $query->createCommand(); 
    $data = $command->queryAll(); 
+0

正しい答えですが、配列形式の応答です。とにかくありがとうございました。 – rmpstmp

0

はjoinWithでは、フィールド、apartments.cityId 'オン' に参加逃し、あなたが自動的に生成されたモデルのYiiで関係を持っているので

$apartments = Apartments::find()->select('apartments.title, city.value, partments.cityId, city.id')->joinWith('city')->all(); 
関連する問題