yiisoft / yiisoft/db

Calling yii\db\Query addSelect($fields) overwrites default select set

Open
#870 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
216
Forks
51
Avg merge
3d 10h
Merged PRs (30d)
2

Description

What steps will reproduce the problem?
$query = ArClass::find();
$query->addSelect('extraField');
$item = $query->one();
What is the expected result?

$item has all default fields + 1 extraField, defined in addSelect()

What do you get instead?

$item has only extraField and does not have default fields.

Additional info
Q A
Yii version 2.0.13.1
PHP version 7.0.23
Operating system Ubuntu 16.04.3 x86_64
  1. Due to docs, select property means "all by default" yii\db\Query:58
 /**
  * @var array the columns being selected. For example, `['id', 'name']`.
  * This is used to construct the SELECT clause in a SQL statement. If not set, it means selecting all columns.
  * @see select()
  */
public $select; 
  1. Add statement ignores defauls if I did not defined defaults again. yii\db\Query:592
if ($this->select === null) {
    $this->select = $columns;
} else {
    $this->select = array_merge($this->select, $columns);
}

Some use cases:

What I want How do I do Is goal Reached?
Select all default columns ArClass::find()->all() // no call select() yes
Select only 2 passed columns ArClass::find()->select(['col1', 'col2'])->all() yes
Select 2 passed columns +1 more ArClass::find()->select(['col1', 'col2'])->addSelect('col3')->all() yes
Select all default columns + 1 more ArClass::find()->addSelect('col3')->all() no
Select all default columns + 1 more ArClass::find()->select('*')->addSelect('col3')->all() sometimes
Select all default columns + 1 more ArClass::find()->select(ArClass::tableName() . '.*')->addSelect('col3')->all() yes

In my opinion, query must select all default columns until I define columns set manually. Add meand adding element to some set. $select defined as all by default, but when you call addSelect() without calling select(), that means default not exists, add this.

See also #12249

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The behavior is in framework/db/Query.php, especially the select property documentation and addSelect() logic around the referenced lines. Reproduce the PHP 7/Yii 2.0.13.1 example, review #12249 and the comment discussion, and verify that the final behavior preserves explicit select() cases while addressing the default-column case.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.