gpt4 book ai didi

php - joinWith 返回空外键值

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:04 25 4
gpt4 key购买 nike

需要使用 joinWith() 方法从 2 个表中选择一些字段。我有 2 个表:公寓和城市。

Apartments 表有 cityId 字段,city 表有 2 个字段:id 和 value。

像这样:

enter image description here

我想从 apartments 表中选择一些字段并通过 cityId 字段获取名称() 城市

公寓模型中的关系:

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

城市模型中的关系:

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

在 Controller 中,我使用 joinWith() 方法构建查询,但它不返回 city.value 字段。

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

print_r($apartments) 返回:

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 ( ) ) ) 

我在此响应中没有看到 city.value 字段,但看到没有任何内容的 city 字段值(value)。

你能帮帮我吗?有什么问题?

最佳答案

试试这个:

$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();

关于php - joinWith 返回空外键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36653056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com