mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 14:28:27 +08:00
Fix #17687: Query::indexBy
can now include a table alias
This commit is contained in:

committed by
Alexander Makarov

parent
d59c60fac0
commit
88f08005ab
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
|||||||
2.0.31 under development
|
2.0.31 under development
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
- Bug #17687: `Query::indexBy` can now include a table alias (brandonkelly)
|
||||||
- Bug #17685: Fix invalid db component in `m180523_151638_rbac_updates_indexes_without_prefix` (rvkulikov)
|
- Bug #17685: Fix invalid db component in `m180523_151638_rbac_updates_indexes_without_prefix` (rvkulikov)
|
||||||
- Bug #17694: Fixed Error Handler to clear registered view tags, scripts, and files when rendering error view through action view (bizley)
|
- Bug #17694: Fixed Error Handler to clear registered view tags, scripts, and files when rendering error view through action view (bizley)
|
||||||
- Bug #17701: Throw `BadRequetHttpException` when request params can’t be bound to `int` and `float` controller action arguments (brandonkelly)
|
- Bug #17701: Throw `BadRequetHttpException` when request params can’t be bound to `int` and `float` controller action arguments (brandonkelly)
|
||||||
|
@ -316,13 +316,21 @@ class Query extends Component implements QueryInterface, ExpressionInterface
|
|||||||
}
|
}
|
||||||
$rows = $this->createCommand($db)->queryAll();
|
$rows = $this->createCommand($db)->queryAll();
|
||||||
$results = [];
|
$results = [];
|
||||||
|
$column = null;
|
||||||
|
if (is_string($this->indexBy)) {
|
||||||
|
if (($dotPos = strpos($this->indexBy, '.')) === false) {
|
||||||
|
$column = $this->indexBy;
|
||||||
|
} else {
|
||||||
|
$column = substr($this->indexBy, $dotPos + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$value = reset($row);
|
$value = reset($row);
|
||||||
|
|
||||||
if ($this->indexBy instanceof \Closure) {
|
if ($this->indexBy instanceof \Closure) {
|
||||||
$results[call_user_func($this->indexBy, $row)] = $value;
|
$results[call_user_func($this->indexBy, $row)] = $value;
|
||||||
} else {
|
} else {
|
||||||
$results[$row[$this->indexBy]] = $value;
|
$results[$row[$column]] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,6 +391,14 @@ abstract class QueryTest extends DatabaseTestCase
|
|||||||
->column($db);
|
->column($db);
|
||||||
$this->assertEquals([3 => 'user3', 2 => 'user2', 1 => 'user1'], $result);
|
$this->assertEquals([3 => 'user3', 2 => 'user2', 1 => 'user1'], $result);
|
||||||
|
|
||||||
|
// https://github.com/yiisoft/yii2/issues/17687
|
||||||
|
$result = (new Query())->from('customer')
|
||||||
|
->select('name')
|
||||||
|
->orderBy(['id' => SORT_DESC])
|
||||||
|
->indexBy('customer.id')
|
||||||
|
->column($db);
|
||||||
|
$this->assertEquals([3 => 'user3', 2 => 'user2', 1 => 'user1'], $result);
|
||||||
|
|
||||||
// https://github.com/yiisoft/yii2/issues/12649
|
// https://github.com/yiisoft/yii2/issues/12649
|
||||||
$result = (new Query())->from('customer')
|
$result = (new Query())->from('customer')
|
||||||
->select(['name', 'id'])
|
->select(['name', 'id'])
|
||||||
|
Reference in New Issue
Block a user