From 33a879ff411928429a5b0188a68eecd9632f87e3 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 9 Oct 2012 10:43:27 +0530 Subject: [PATCH] Fix find('count') with 'group' when result has only one group. Closes #1677 --- lib/Cake/Model/Model.php | 4 ++-- lib/Cake/Test/Case/Model/ModelReadTest.php | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index b597ae2970e..9dd82b6b5d6 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -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']); } diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 7209e021672..6f14e47a208 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -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); } /** @@ -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();