Skip to content
This repository
Browse code

Fix find('count') with 'group' when result has only one group. Closes #…

  • Loading branch information...
commit 33a879ff411928429a5b0188a68eecd9632f87e3 1 parent 1234f96
ADmad authored October 09, 2012
4  lib/Cake/Model/Model.php
@@ -2749,8 +2749,8 @@ protected function _findCount($state, $query, $results = array()) {
2749 2749
 		} elseif ($state === 'after') {
2750 2750
 			foreach (array(0, $this->alias) as $key) {
2751 2751
 				if (isset($results[0][$key]['count'])) {
2752  
-					if (($count = count($results)) > 1) {
2753  
-						return $count;
  2752
+					if ($query['group']) {
  2753
+						return count($results);
2754 2754
 					} else {
2755 2755
 						return intval($results[0][$key]['count']);
2756 2756
 					}
13  lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -6860,6 +6860,17 @@ public function testFindCount() {
6860 6860
 		));
6861 6861
 		$result = $Article->find('count', array('group' => array('Article.user_id')));
6862 6862
 		$this->assertEquals($expected, $result);
  6863
+
  6864
+		$expected = count($Article->find('all', array(
  6865
+			'fields' => array('Article.user_id'),
  6866
+			'conditions' => array('Article.user_id' => 1),
  6867
+			'group' => 'Article.user_id')
  6868
+		));
  6869
+		$result = $Article->find('count', array(
  6870
+			'conditions' => array('Article.user_id' => 1),
  6871
+			'group' => array('Article.user_id'),
  6872
+		));
  6873
+		$this->assertEquals($expected, $result);
6863 6874
 	}
6864 6875
 
6865 6876
 /**
@@ -6886,7 +6897,7 @@ public function testFindCountDistinct() {
6886 6897
 		$this->skipIf($this->db instanceof Sqlite, 'SELECT COUNT(DISTINCT field) is not compatible with SQLite.');
6887 6898
 		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
6888 6899
 
6889  
-		$this->loadFixtures('Project');
  6900
+		$this->loadFixtures('Project', 'Thread');
6890 6901
 		$TestModel = new Project();
6891 6902
 		$TestModel->create(array('name' => 'project')) && $TestModel->save();
6892 6903
 		$TestModel->create(array('name' => 'project')) && $TestModel->save();

0 notes on commit 33a879f

Please sign in to comment.
Something went wrong with that request. Please try again.