Yii2: Query::select removes fields if it passed twice

Created on 19 Feb 2018  路  5Comments  路  Source: yiisoft/yii2

After updating from 2.0.13 to 2.0.14 i have noticed some strange behavior of Query::select().
If you reassign select fields, duplicates will be removed like in example below:

What steps will reproduce the problem?

$query = (new yii\db\Query())
            ->select('id')
            ->from('place')
        ;
$query->select(['id', 'brand_id']);
return $query->one();

What is the expected result?

[
'id' => 1,
'brand_id' => 1,
]

What do you get instead?

[
'brand_id' => 1,
]

Additional info

Notice: if define id inside select with alias in first __or__ second definition (i.e ['id_alias' => 'id']), it will be ok and id will be selected.

It seems that such behavior occurs because of Query::getUniqueColumns(), added in 2.0.14.

Maybe related with: #15674

| Q | A
| ---------------- | ---
| Yii version | 2.0.14
| PHP version | 7.1.14
| DB | mysql 5.7

bug

Most helpful comment

with fix isn't worked on select's like

->select([
'prefix' => 'LEFT(name, 7)',
'prefix_key' => 'LEFT(name, 7)',
])

because array_unique filtered out without compare array keys

All 5 comments

Fixed, thank you

with fix isn't worked on select's like

->select([
'prefix' => 'LEFT(name, 7)',
'prefix_key' => 'LEFT(name, 7)',
])

because array_unique filtered out without compare array keys

Could you open a separate issue, please?

Thank you

Was this page helpful?
0 / 5 - 0 ratings