From ce4fa20f3fcca8e26cd36f4aef62319a7340344f Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 15 Feb 2009 21:05:42 +0000 Subject: [PATCH] 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 --- cake/libs/model/datasources/dbo_source.php | 7 ++++--- .../libs/model/datasources/dbo_source.test.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index b1a80f75e21..6b2e5cfae83 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -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']; diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 584f8006ab1..fc3cef0df8a 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -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 *