Skip to content

Commit

Permalink
Fix find('count') with 'group' when result has only one group. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 9, 2012
1 parent 1234f96 commit 33a879f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Model/Model.php
Expand Up @@ -2749,8 +2749,8 @@ protected function _findCount($state, $query, $results = array()) {
} elseif ($state === 'after') {
foreach (array(0, $this->alias) as $key) {
if (isset($results[0][$key]['count'])) {
if (($count = count($results)) > 1) {
return $count;
if ($query['group']) {
return count($results);
} else {
return intval($results[0][$key]['count']);
}
Expand Down
13 changes: 12 additions & 1 deletion lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -6860,6 +6860,17 @@ public function testFindCount() {
));
$result = $Article->find('count', array('group' => array('Article.user_id')));
$this->assertEquals($expected, $result);

$expected = count($Article->find('all', array(
'fields' => array('Article.user_id'),
'conditions' => array('Article.user_id' => 1),
'group' => 'Article.user_id')
));
$result = $Article->find('count', array(
'conditions' => array('Article.user_id' => 1),
'group' => array('Article.user_id'),
));
$this->assertEquals($expected, $result);
}

/**
Expand All @@ -6886,7 +6897,7 @@ public function testFindCountDistinct() {
$this->skipIf($this->db instanceof Sqlite, 'SELECT COUNT(DISTINCT field) is not compatible with SQLite.');
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');

$this->loadFixtures('Project');
$this->loadFixtures('Project', 'Thread');
$TestModel = new Project();
$TestModel->create(array('name' => 'project')) && $TestModel->save();
$TestModel->create(array('name' => 'project')) && $TestModel->save();
Expand Down

0 comments on commit 33a879f

Please sign in to comment.