Making Model::find('count') behave nicely when 'group' key is specified in options. Closes #1677

This commit is contained in:
ADmad 2011-08-14 07:29:21 +05:30
parent 5d79299362
commit ef4826eb70
2 changed files with 15 additions and 7 deletions

View file

@ -2321,10 +2321,14 @@ class Model extends Object {
$query['order'] = false; $query['order'] = false;
return $query; return $query;
} elseif ($state === 'after') { } elseif ($state === 'after') {
if (isset($results[0][0]['count'])) { foreach (array(0, $this->alias) as $key) {
return intval($results[0][0]['count']); if (isset($results[0][$key]['count'])) {
} elseif (isset($results[0][$this->alias]['count'])) { if (count($results) > 1) {
return intval($results[0][$this->alias]['count']); return intval(array_sum(Set::extract('/' . $key . '/count', $results)));
} else {
return intval($results[0][$key]['count']);
}
}
} }
return false; return false;
} }

View file

@ -389,7 +389,7 @@ class ModelReadTest extends BaseModelTest {
*/ */
public function testRecursiveUnbind() { 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->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'); $this->loadFixtures('Apple', 'Sample');
$TestModel = new Apple(); $TestModel = new Apple();
$TestModel->recursive = 2; $TestModel->recursive = 2;
@ -3645,7 +3645,7 @@ class ModelReadTest extends BaseModelTest {
*/ */
public function testFindCombinedRelations() { 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->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'); $this->loadFixtures('Apple', 'Sample');
$TestModel = new Apple(); $TestModel = new Apple();
@ -6604,7 +6604,7 @@ class ModelReadTest extends BaseModelTest {
* @return void * @return void
*/ */
public function testFindCount() { public function testFindCount() {
$this->loadFixtures('User', 'Project'); $this->loadFixtures('User', 'Article');
$TestModel = new User(); $TestModel = new User();
$this->db->getLog(false, true); $this->db->getLog(false, true);
@ -6620,6 +6620,10 @@ class ModelReadTest extends BaseModelTest {
$log = $this->db->getLog(); $log = $this->db->getLog();
$this->assertTrue(isset($log['log'][0]['query'])); $this->assertTrue(isset($log['log'][0]['query']));
$this->assertNoPattern('/ORDER\s+BY/', $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);
} }
/** /**