From ef4826eb7052c21dd75559ca8571019d0651e77a Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 14 Aug 2011 07:29:21 +0530 Subject: [PATCH] Making Model::find('count') behave nicely when 'group' key is specified in options. Closes #1677 --- lib/Cake/Model/Model.php | 12 ++++++++---- lib/Cake/Test/Case/Model/ModelReadTest.php | 10 +++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index d88549c9b..47a84f3f9 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2321,10 +2321,14 @@ class Model extends Object { $query['order'] = false; return $query; } elseif ($state === 'after') { - if (isset($results[0][0]['count'])) { - return intval($results[0][0]['count']); - } elseif (isset($results[0][$this->alias]['count'])) { - return intval($results[0][$this->alias]['count']); + foreach (array(0, $this->alias) as $key) { + if (isset($results[0][$key]['count'])) { + if (count($results) > 1) { + return intval(array_sum(Set::extract('/' . $key . '/count', $results))); + } else { + return intval($results[0][$key]['count']); + } + } } return false; } diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 7d7ee98b7..d2fd6ea76 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -389,7 +389,7 @@ class ModelReadTest extends BaseModelTest { */ public function testRecursiveUnbind() { $this->skipIf($this->db instanceof Sqlserver, 'The test of testRecursiveUnbind test is not compatible with SQL Server, because it check for time columns.'); - + $this->loadFixtures('Apple', 'Sample'); $TestModel = new Apple(); $TestModel->recursive = 2; @@ -3645,7 +3645,7 @@ class ModelReadTest extends BaseModelTest { */ public function testFindCombinedRelations() { $this->skipIf($this->db instanceof Sqlserver, 'The test of testRecursiveUnbind test is not compatible with SQL Server, because it check for time columns.'); - + $this->loadFixtures('Apple', 'Sample'); $TestModel = new Apple(); @@ -6604,7 +6604,7 @@ class ModelReadTest extends BaseModelTest { * @return void */ public function testFindCount() { - $this->loadFixtures('User', 'Project'); + $this->loadFixtures('User', 'Article'); $TestModel = new User(); $this->db->getLog(false, true); @@ -6620,6 +6620,10 @@ class ModelReadTest extends BaseModelTest { $log = $this->db->getLog(); $this->assertTrue(isset($log['log'][0]['query'])); $this->assertNoPattern('/ORDER\s+BY/', $log['log'][0]['query']); + + $Article = new Article(); + $result = $Article->find('count', array('group' => 'Article.user_id')); + $this->assertEqual($result, 3); } /**