#1005674 by solotandem: Fixed SelectQuery::countQuery() fails with a group by expression

merge-requests/26/head
Angie Byron 2010-12-28 18:49:51 +00:00
parent c719b1a5b3
commit fbd6ffbf52
2 changed files with 6 additions and 3 deletions

View File

@ -1354,7 +1354,7 @@ class SelectQuery extends Query implements SelectQueryInterface {
} }
public function groupBy($field) { public function groupBy($field) {
$this->group[] = $field; $this->group[$field] = $field;
return $this; return $this;
} }
@ -1362,7 +1362,7 @@ class SelectQuery extends Query implements SelectQueryInterface {
// Create our new query object that we will mutate into a count query. // Create our new query object that we will mutate into a count query.
$count = clone($this); $count = clone($this);
$group_by = drupal_map_assoc(array_values($count->getGroupBy())); $group_by = $count->getGroupBy();
if (!$count->distinct) { if (!$count->distinct) {
// When not executing a distinct query, we can zero-out existing fields // When not executing a distinct query, we can zero-out existing fields

View File

@ -2033,10 +2033,13 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase {
$this->assertEqual($count, 3, t('Counted the correct number of records.')); $this->assertEqual($count, 3, t('Counted the correct number of records.'));
// Test wth an alias. // Use a column alias as, without one, the query can succeed for the wrong
// reason.
$query = db_select('test_task'); $query = db_select('test_task');
$pid_field = $query->addField('test_task', 'pid', 'pid_alias'); $pid_field = $query->addField('test_task', 'pid', 'pid_alias');
$query->addExpression('COUNT(test_task.task)', 'count');
$query->groupBy('pid_alias'); $query->groupBy('pid_alias');
$query->orderBy('pid_alias', 'asc');
$count = $query->countQuery()->execute()->fetchField(); $count = $query->countQuery()->execute()->fetchField();