yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

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

Open dicrtarasov opened this issue 6 years ago • 2 comments

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

dicrtarasov avatar Aug 11 '19 21:08 dicrtarasov

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

NingerJohn avatar Mar 29 '23 04:03 NingerJohn

A bug, it seems.

samdark avatar Mar 29 '23 06:03 samdark