$model = Account::find() ->select([ 'campaign_id', //Как тут указать имя таблицы указанной у модели Campaign? 'name', ]) ->innerJoinWith(['campaign']) ->where(['account_id' => $accountID]) ->asArray() ->all(); 

Relay looks like this

 /** * @return \yii\db\ActiveQuery */ public function getCampaign() { return $this->hasOne(Campaign::className(), ['campaign_id' => 'campaign_id']); } 

If you do not explicitly specify from which table to take campaign_id , then we get an error

SQLSTATE [23000]: Integrity constraint violation: 1052 Column 'campaign_id' in field list is ambiguous

Which is basically logical. But if you explicitly specify the name of the campaign.campaign_id table, then a dependency appears that you wanted to get rid of using the model. (In a specific case, adding the prefix tmp_ to the name of the campaign table, so as not to edit all requests, but only change the value of tableName in the model)

  • Now somehow did so. Campaign::tableName() . '.campaign_id' Campaign::tableName() . '.campaign_id' But I think that there is some more correct solution. - Ninazu

1 answer 1

In general, the easiest way through alias IMHO

 Account::find() ->select([ 'c.campaign_id', 'a.name', ]) ->from(Account::tableName() . ' a') ->innerJoinWith(['campaign c']) ->where(['account_id' => $accountID]) ->asArray() ->all();