Skip to content

Commit

Permalink
Fixing escaping of fields in hasAny() also adding in correct model al…
Browse files Browse the repository at this point in the history
…iases.

Tests added. 
Fixes #6089

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8032 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
markstory committed Feb 15, 2009
1 parent 43ad3d7 commit ce4fa20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -2102,10 +2102,11 @@ function close() {
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'];
Expand Down
14 changes: 14 additions & 0 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -3394,6 +3394,20 @@ function testBuildColumn() {
$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
*
Expand Down

0 comments on commit ce4fa20

Please sign in to comment.