joinWith返回null外键值

时间:2016-04-15 17:07:58

标签: php mysql yii2

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

公寓表有cityId字段,城市表有2个字段:id和value。

这样的事情:

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)会返回此信息:

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 字段值。

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

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

由于你在模型中有关系yii自动生成连接值。 试试这段代码

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

对不起,如果这不是你的意思!

答案 2 :(得分:0)

在joinWith中,你错过了加入' on' fields,apartments.cityId和city.id

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