Fixed yii\db\Query::count() fails for query containing 'having' without 'group by'

This commit is contained in:
Klimov Paul
2015-04-14 16:11:07 +03:00
parent 68d287f5d4
commit b8081b59b3
3 changed files with 33 additions and 1 deletions

View File

@ -21,6 +21,7 @@ Yii Framework 2 Change Log
- Bug #8012: Fixed fetching multiple relations between two tables for pgsql (nineinchnick) - Bug #8012: Fixed fetching multiple relations between two tables for pgsql (nineinchnick)
- Bug #8014: Fixed setting incorrect form "action" property after submitting a form using a link with "data-method" and containing "action" among "data-params" (samdark) - Bug #8014: Fixed setting incorrect form "action" property after submitting a form using a link with "data-method" and containing "action" among "data-params" (samdark)
- Bug #8032: `yii\rbac\PhpManager::updateItem()` was unable to rename item updated (ChristopheBrun, samdark) - Bug #8032: `yii\rbac\PhpManager::updateItem()` was unable to rename item updated (ChristopheBrun, samdark)
- Bug #8068: Fixed `yii\db\Query::count()` fails for query containing 'having' without 'group by' (klimov-paul)
- Enh #3376: Added `yii\validators\EachValidator`, which allows validation of the array attributes (klimov-paul) - Enh #3376: Added `yii\validators\EachValidator`, which allows validation of the array attributes (klimov-paul)
- Enh #6895: Added `ignoreCategories` config option for message command to ignore categories specified (samdark) - Enh #6895: Added `ignoreCategories` config option for message command to ignore categories specified (samdark)
- Enh #6975: Pressing arrows while focused in inputs of Active Form with `validateOnType` enabled no longer triggers validation (slinstj) - Enh #6975: Pressing arrows while focused in inputs of Active Form with `validateOnType` enabled no longer triggers validation (slinstj)

View File

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

View File

@ -205,4 +205,35 @@ 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);
} }
public function testCount()
{
$db = $this->getConnection();
$count = (new Query)->from('customer')->count('*', $db);
$this->assertEquals(3, $count);
$count = (new Query)->from('customer')->where(['status' => 2])->count('*', $db);
$this->assertEquals(1, $count);
$count = (new Query)->from('customer')->groupBy('status')->count('*', $db);
$this->assertEquals(2, $count);
}
/**
* @see https://github.com/yiisoft/yii2/issues/8068
*
* @depends testCount
*/
public function testCountHaving()
{
if (in_array($this->driverName, ['sqlite'])) {
$this->markTestSkipped("{$this->driverName} does not support having without group by.");
}
$db = $this->getConnection();
$count = (new Query)->from('customer')->having(['status' => 2])->count('*', $db);
$this->assertEquals(1, $count);
}
} }