diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3af92e6f0e..4d176fe753 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/db/Query.php b/framework/db/Query.php index 5b4e249972..2b1fa47cc7 100644 --- a/framework/db/Query.php +++ b/framework/db/Query.php @@ -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]) diff --git a/tests/framework/db/QueryTest.php b/tests/framework/db/QueryTest.php index 95ce860506..84538a5201 100644 --- a/tests/framework/db/QueryTest.php +++ b/tests/framework/db/QueryTest.php @@ -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); } /**