refs #11122: Fixed can not use orderBy with aggregate functions (#12772)

* refs #11122: Fixed can not use `orderBy` with aggregate functions like `count`, `max` etc.

* Update QueryTest.php
This commit is contained in:
Pavel Chaplygin
2016-12-18 16:28:34 +07:00
committed by Dmitry Naumenko
parent a710345ea7
commit 25e6eb04a5
3 changed files with 12 additions and 1 deletions

View File

@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Bug #9305: Fixed MSSQL `Schema::TYPE_TIMESTAMP` to be 'datetime' instead of 'timestamp', which is just an incremental number (nkovacs)
- Bug #9616: Fixed mysql\Schema::loadColumnSchema to set enumValues attribute correctly if enum definition contains commas (fphammerle)
- Bug #9796: Initialization of not existing `yii\grid\ActionColumn` default buttons (arogachev)
- Bug #11122: Fixed can not use `orderBy` with aggregate functions like `count` (Ni-san)
- Bug #11771: Fixed semantics of `yii\di\ServiceLocator::__isset()` to match the behavior of `__get()` which fixes inconsistent behavior on newer PHP versions (cebe)
- Bug #12213: Fixed `yii\db\ActiveRecord::unlinkAll()` to respect `onCondition()` of the relational query (silverfire)
- Bug #12681: Changed `data` column type from `text` to `blob` to handle null-byte (`\0`) in serialized RBAC rule properly (silverfire)

View File

@ -421,7 +421,13 @@ class Query extends Component implements QueryInterface
$this->limit = $limit;
$this->offset = $offset;
if (empty($this->groupBy) && empty($this->having) && empty($this->union) && !$this->distinct) {
if (
!$this->distinct
&& empty($this->groupBy)
&& empty($this->having)
&& empty($this->union)
&& empty($this->orderBy)
) {
return $command->queryScalar();
} else {
return (new Query)->select([$selectExpression])

View File

@ -319,6 +319,10 @@ abstract class QueryTest extends DatabaseTestCase
$count = (new Query)->select('[[status]], COUNT([[id]])')->from('customer')->groupBy('status')->count('*', $db);
$this->assertEquals(2, $count);
// testing that orderBy() should be ignored here as it does not affect the count anyway.
$count = (new Query)->from('customer')->orderBy('status')->count('*', $db);
$this->assertEquals(3, $count);
}
/**