Browse Source

Reversed #18499 (#18546)

tags/2.0.41.1
Bizley 4 years ago committed by GitHub
parent
commit
6dad27d4a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      docs/guide/db-query-builder.md
  2. 4
      framework/CHANGELOG.md
  3. 2
      framework/db/ActiveQueryInterface.php
  4. 20
      framework/db/Query.php
  5. 2
      framework/db/QueryInterface.php
  6. 2
      framework/db/QueryTrait.php
  7. 21
      tests/framework/db/ActiveRecordTest.php
  8. 19
      tests/framework/db/QueryTest.php

3
docs/guide/db-query-builder.md

@ -716,7 +716,8 @@ $query = (new \yii\db\Query())
->all(); ->all();
``` ```
The column name passed into [[yii\db\Query::indexBy()|indexBy()]] method has to be a part of the `SELECT` fragment of a SQL statement. If [[yii\db\Query::select()|select()]] is not used, all columns are selected and therefore the condition is met. If [[yii\db\Query::select()|select()]] is used with an array in its parameter, Yii handles adding that required SQL fragment for you. This applies when using [[yii\db\Query::indexBy()|indexBy()]] with [[yii\db\Query::all()|all()]] or [[yii\db\Query::column()|column()]]. In other cases, like the following example with an anonymous function, is up to users themselves to take care of it. The column which name is passed into [[yii\db\Query::indexBy()|indexBy()]] method must be present in the result set in order
for indexing to work - it is up to the developer to take care of it.
To index by expression values, pass an anonymous function to the [[yii\db\Query::indexBy()|indexBy()]] method: To index by expression values, pass an anonymous function to the [[yii\db\Query::indexBy()|indexBy()]] method:

4
framework/CHANGELOG.md

@ -1,10 +1,10 @@
Yii Framework 2 Change Log Yii Framework 2 Change Log
========================== ==========================
2.0.42 under development 2.0.41.1 under development
------------------------ ------------------------
- no changes in this release. - Bug #18545: Reversed changes made to the `yii\db\Query::all()` and `indexBy` handling (bizley)
2.0.41 March 03, 2021 2.0.41 March 03, 2021

2
framework/db/ActiveQueryInterface.php

@ -53,8 +53,6 @@ interface ActiveQueryInterface extends QueryInterface
* // return the index value corresponding to $model * // return the index value corresponding to $model
* } * }
* ``` * ```
* The column has to be a part of the `SELECT` fragment of a SQL statement.
* If [[yii\db\Query::select()|select()]] is used with an array in its parameter, Yii handles adding that required SQL fragment for you.
* *
* @return $this the query object itself * @return $this the query object itself
*/ */

20
framework/db/Query.php

@ -246,26 +246,6 @@ class Query extends Component implements QueryInterface, ExpressionInterface
return []; return [];
} }
if (is_string($this->indexBy) && $this->indexBy && is_array($this->select)) {
$isIndexByAnArray = false;
if (strpos($this->indexBy, '.')) {
$indexByParts = explode('.', $this->indexBy);
foreach ($indexByParts as $indexByPart) {
if (is_numeric($indexByPart)) {
$isIndexByAnArray = true;
break;
}
}
}
if (!$isIndexByAnArray && !in_array($this->indexBy, $this->select, true)) {
if (strpos($this->indexBy, '.') === false && count($tables = $this->getTablesUsedInFrom()) > 0) {
$this->select[] = key($tables) . '.' . $this->indexBy;
} else {
$this->select[] = $this->indexBy;
}
}
}
$rows = $this->createCommand($db)->queryAll(); $rows = $this->createCommand($db)->queryAll();
return $this->populate($rows); return $this->populate($rows);

2
framework/db/QueryInterface.php

@ -68,8 +68,6 @@ interface QueryInterface
* // return the index value corresponding to $row * // return the index value corresponding to $row
* } * }
* ``` * ```
* The column has to be a part of the `SELECT` fragment of a SQL statement.
* If [[yii\db\Query::select()|select()]] is used with an array in its parameter, Yii handles adding that required SQL fragment for you.
* *
* @return $this the query object itself * @return $this the query object itself
*/ */

2
framework/db/QueryTrait.php

@ -71,8 +71,6 @@ trait QueryTrait
* // return the index value corresponding to $row * // return the index value corresponding to $row
* } * }
* ``` * ```
* The column has to be a part of the `SELECT` fragment of a SQL statement.
* If [[yii\db\Query::select()|select()]] is used with an array in its parameter, Yii handles adding that required SQL fragment for you.
* *
* @return $this the query object itself * @return $this the query object itself
*/ */

21
tests/framework/db/ActiveRecordTest.php

@ -2095,25 +2095,4 @@ abstract class ActiveRecordTest extends DatabaseTestCase
} }
} }
} }
/**
* @see https://github.com/yiisoft/yii2/issues/18525
*/
public function testHasManyWithIndexBy()
{
$category = Category::find()->joinWith('items')->indexBy('items.0.name');
$this->assertEquals(['Agile Web Application Development with Yii1.1 and PHP5', 'Ice Age'], array_keys($category->all()));
$category = Category::find()->select([Category::tableName() . '.*'])->joinWith('items')->indexBy('items.0.name');
$this->assertEquals(['Agile Web Application Development with Yii1.1 and PHP5', 'Ice Age'], array_keys($category->all()));
$category = Category::find()->select([Category::tableName() . '.*'])->joinWith('items')->indexBy('name');
$this->assertEquals(['Books', 'Movies'], array_keys($category->all()));
$category = Category::find()->joinWith('items')->indexBy('item.name');
$this->assertEquals([''], array_keys($category->all()));
$category = Category::find()->select([Category::tableName() . '.name'])->joinWith('items')->indexBy('id');
$this->assertEquals([1, 2], array_keys($category->all()));
}
} }

19
tests/framework/db/QueryTest.php

@ -795,23 +795,4 @@ abstract class QueryTest extends DatabaseTestCase
$newQuery->withQueries $newQuery->withQueries
); );
} }
/**
* @see https://github.com/yiisoft/yii2/issues/18499
*/
public function testAllWithAutomaticallyAddedIndexedByColumn()
{
$db = $this->getConnection();
$result = (new Query())->from('customer')
->select('name')
->orderBy(['id' => SORT_DESC])
->indexBy('id')
->all($db);
$this->assertEquals([
3 => ['name' => 'user3', 'id' => 3],
2 => ['name' => 'user2', 'id' => 2],
1 => ['name' => 'user1', 'id' => 1]
], $result);
}
} }

Loading…
Cancel
Save