Yii2: Improve unclear upgrade warning for Query::select()

Created on 27 Jun 2019  路  5Comments  路  Source: yiisoft/yii2

The warning that you get when upgrading to 2.0.20 is in my opinion very unclear:

yii\db\Query::select() and addSelect() now normalize the columns when saving them to $this->select, so code that works directly with that property directly may need to be modified.

What does that mean? What is "normalize" in this regard (the word "normalize" is not even mentioned once on the respective guide page). The docs for the Query::normalizeSelect() don't help either:

Normalizes the SELECT columns passed to [[select()]] or [[addSelect()]].

Can we please get a more clear message here including examples etc.? The warning should also include bad examples that require action.

bug

Most helpful comment

for example:

->select(['COUNT(test.field1) AS my_count, field2'])//BC

such a record will lead to the generation of incorrect SQL
Before 2.0.21

SELECT COUNT(test.field1) AS my_count, field2

after

SELECT COUNT(test.field1) AS my_count, AS `field2`

You can rewrite this entry so:

->select(['my_count' => 'COUNT(test.field1)', 'field2']) //you can write now

All 5 comments

for example:

->select(['COUNT(test.field1) AS my_count, field2'])//BC

such a record will lead to the generation of incorrect SQL
Before 2.0.21

SELECT COUNT(test.field1) AS my_count, field2

after

SELECT COUNT(test.field1) AS my_count, AS `field2`

You can rewrite this entry so:

->select(['my_count' => 'COUNT(test.field1)', 'field2']) //you can write now

@s1lver the thing you've mentioned is now fixed.

There is still an issue with the update.

Code ->select(['rating' => 'AVG(rating)']) generates following SQL SELECT AVG(rating) AS AVG_rating AS rating.

Setting the table name fixes the issue, but it should not be necessary for single table queries:
->select(['rating' => 'AVG(table_name.rating)']) generates working SQL SELECT AVG(table_name.rating) AS rating.

Not sure if this is intended or not...

Does not look like it's alright.

I can't replay this problem. What am I doing wrong?

$query = (new Query())
 ->select(['rating' => 'AVG(rating)'])
 ->from('tablename');
list($sql, $params) = $this->getQueryBuilder()->build($query);
$this->assertEquals($sql, 'SELECT AVG(rating) AS "rating" FROM "tablename"');

Returns OK.

Was this page helpful?
0 / 5 - 0 ratings