yii2
yii2 copied to clipboard
yii2\db\ActiveQuery::onCondition not using table name and/or aliases in SQL
Have some relation with onCondition:
public function getDesc() {
return $this->hasOne(Desc::class, ['blog_id' => 'id'])
->onCondition(['lang' => \Yii::$app->language]);
}
Then using with aliases:
$blog->joinWith(['desc' => function($q) {
return $q->alias('myalias');
}];
Generated SQL:
SELECT `myalias`.* FROM `blog` `myalias` LEFT JOIN `blog_desc` ON (`myalias`.`id` = `blog_desc`.`blog_id`) AND (`lang`='ru')
There is no table name or table alias at (lang='ru'), so:
- can't using chained joins with different aliases;
- have conflicts if "lang" belongs to different tables;
The right way is using table name or current alias before field name.
| ---------------- | --- | Yii version | 2.0.24
Same issue, same curious about whether it's the bug or the missed feature?
A bug, it seems.