Skip to content

Commit

Permalink
Making Model::find('count') behave nicely when 'group' key is specifi…
Browse files Browse the repository at this point in the history
…ed in options. Closes #1677
  • Loading branch information
ADmad committed Aug 14, 2011
1 parent 5d79299 commit ef4826e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/Cake/Model/Model.php
Expand Up @@ -2321,10 +2321,14 @@ protected function _findCount($state, $query, $results = array()) {
$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;
}
Expand Down
10 changes: 7 additions & 3 deletions lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -389,7 +389,7 @@ public function testVeryStrangeUseCase() {
*/
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;
Expand Down Expand Up @@ -3645,7 +3645,7 @@ public function testFindNeighbors() {
*/
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();

Expand Down Expand Up @@ -6604,7 +6604,7 @@ public function testFindUnique() {
* @return void
*/
public function testFindCount() {
$this->loadFixtures('User', 'Project');
$this->loadFixtures('User', 'Article');

$TestModel = new User();
$this->db->getLog(false, true);
Expand All @@ -6620,6 +6620,10 @@ public function testFindCount() {
$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);
}

/**
Expand Down

0 comments on commit ef4826e

Please sign in to comment.