joinWith()
メソッドを使用して2つのテーブルからいくつかのフィールドを選択する必要があります。私は2つのテーブルを持っています:アパートと都市。joinWith null null外部キー値
アパートテーブルはcityIdフィールドを持ち、市の表は、2つのフィールドがあります:idと値を。このような
何か:
私は アパートテーブルからいくつかのフィールドを選択したいと経由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 ()))
。
私を助けることができますか?問題は何ですか?
ああ、ありがとう。 – rmpstmp
喜んで:) – Midhun