yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

yii2\db\ActiveQuery::onCondition not using table name and/or aliases in SQL #17506

Open dicrtarasov opened 5 years ago

dicrtarasov commented 5 years ago

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: 1) can't using chained joins with different aliases; 2) 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

NingerJohn commented 1 year ago

Same issue, same curious about whether it's the bug or the missed feature?

samdark commented 1 year ago

A bug, it seems.