Fixing escaping of fields in hasAny() also adding in correct model aliases.

Tests added. 
Fixes #6089

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8032 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2009-02-15 21:05:42 +00:00
parent 43ad3d76e5
commit ce4fa20f3f
2 changed files with 18 additions and 3 deletions

View file

@ -2102,10 +2102,11 @@ class DboSource extends DataSource {
function hasAny(&$Model, $sql) {
$sql = $this->conditions($sql);
$table = $this->fullTableName($Model);
$where = $sql ? "WHERE {$sql}" : 'WHERE 1 = 1';
$id = $Model->primaryKey;
$alias = $this->alias . $this->name($Model->alias);
$where = $sql ? "{$sql}" : ' WHERE 1 = 1';
$id = $Model->escapeField();
$out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$where}");
$out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$alias}{$where}");
if (is_array($out)) {
return $out[0]['count'];

View file

@ -3394,6 +3394,20 @@ class DboSourceTest extends CakeTestCase {
$expected = '`int_field` int(11) NOT NULL';
$this->assertTrue($result, $expected);
}
/**
* test hasAny()
*
* @return void
**/
function testHasAny() {
$this->testDb->hasAny($this->Model, array());
$expected = 'SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1';
$this->assertEqual($this->testDb->simulated[0], $expected);
$this->testDb->hasAny($this->Model, array('TestModel.name' => 'harry'));
$expected = "SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = 'harry'";
$this->assertEqual($this->testDb->simulated[1], $expected);
}
/**
* testIntrospectType method
*