Updating findCount() calls to find('count') in Model test, fixing custom field counting support, regex typo in DataSource

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7466 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-08-14 16:52:29 +00:00
parent 7d9af97bd0
commit dfc115d5da
3 changed files with 25 additions and 11 deletions

View file

@ -199,7 +199,7 @@ class DataSource extends Object {
}
$key = ConnectionManager::getSourceName($this) . '_' . $this->config['database'] . '_list';
$key = preg_replace('/[^A-Za-z0-9_\-\.+]/', '_', $key);
$key = preg_replace('/[^A-Za-z0-9_\-.+]/', '_', $key);
$sources = Cache::read($key, '_cake_model_');
if (empty($sources)) {

View file

@ -1838,6 +1838,8 @@ class Model extends Overloadable {
if (empty($query['fields'])) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$query['fields'] = $db->calculate($this, 'count');
} elseif (is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) {
$query['fields'] = "COUNT({$query['fields']}) as count";
}
$query['order'] = false;
return $query;

View file

@ -873,7 +873,7 @@ class ModelTest extends CakeTestCase {
$result = $Primary->field('primary_name', array('id' => $result));
$this->assertEqual($result, 'Primary Name New');
$result = $Primary->findCount();
$result = $Primary->find('count');
$this->assertEqual($result, 2);
}
/**
@ -1102,9 +1102,12 @@ class ModelTest extends CakeTestCase {
$expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article');
$this->assertEqual($result, $expected);
$result = $TestModel->find('list', array('order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC')));
$expected = array(1 => 'First Article', 3 => 'Third Article', 2 => 'Second Article');
$this->assertEqual($result, $expected);
$db =& ConnectionManager::getDataSource('test_suite');
if ($db->config['driver'] == 'mysql') {
$result = $TestModel->find('list', array('order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC')));
$expected = array(1 => 'First Article', 3 => 'Third Article', 2 => 'Second Article');
$this->assertEqual($result, $expected);
}
$result = Set::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title'))), '{n}.Article.id', '{n}.Article.title');
$expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article');
@ -1471,16 +1474,17 @@ class ModelTest extends CakeTestCase {
* @return void
*/
function testFindCount() {
$this->loadFixtures('User');
$this->loadFixtures('User', 'Project');
$TestModel =& new User();
$result = $TestModel->findCount();
$result = $TestModel->find('count');
$this->assertEqual($result, 4);
$fullDebug = $this->db->fullDebug;
$this->db->fullDebug = true;
$TestModel->order = 'User.id';
$this->db->_queriesLog = array();
$result = $TestModel->findCount();
$result = $TestModel->find('count');
$this->assertEqual($result, 4);
$this->assertTrue(isset($this->db->_queriesLog[0]['query']));
@ -1488,6 +1492,14 @@ class ModelTest extends CakeTestCase {
$this->db->_queriesLog = array();
$this->db->fullDebug = $fullDebug;
$TestModel =& new Project();
$TestModel->create(array('name' => 'project')) && $TestModel->save();
$TestModel->create(array('name' => 'project')) && $TestModel->save();
$TestModel->create(array('name' => 'project')) && $TestModel->save();
$result = $TestModel->find('count', array('fields' => 'DISTINCT Project.name'));
$this->assertEqual($result, 4);
}
/**
* testFindMagic method
@ -3371,13 +3383,13 @@ class ModelTest extends CakeTestCase {
$result = $TestModel->Comment->Attachment->read(null, 1);
$this->assertFalse($result);
$result = $TestModel->findCount();
$result = $TestModel->find('count');
$this->assertEqual($result, 2);
$result = $TestModel->Comment->findCount();
$result = $TestModel->Comment->find('count');
$this->assertEqual($result, 4);
$result = $TestModel->Comment->Attachment->findCount();
$result = $TestModel->Comment->Attachment->find('count');
$this->assertEqual($result, 0);
}
/**