diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index ef4bab7e7..5670adab9 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -2490,16 +2490,16 @@ class DboSource extends DataSource { $count = count($value); if ($count === 1 && !preg_match("/\s+NOT$/", $key)) { $data = $this->_quoteFields($key) . ' = ('; - } else { - $data = $this->_quoteFields($key) . ' IN ('; - } - if ($quoteValues) { - if (is_object($model)) { - $columnType = $model->getColumnType($key); + if ($quoteValues) { + if (is_object($model)) { + $columnType = $model->getColumnType($key); + } + $data .= implode(', ', $this->value($value, $columnType)); } - $data .= implode(', ', $this->value($value, $columnType)); + $data .= ')'; + } else { + $data = $this->_parseKey($model, $key, $value); } - $data .= ')'; } else { $ret = $this->conditionKeysToString($value, $quoteValues, $model); if (count($ret) > 1) { diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 4f3baa0bd..7ae749268 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -1960,7 +1960,7 @@ class MysqlTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Dbo->conditions(array('score' => array(2 => 1, 2, 10))); - $expected = " WHERE score IN (1, 2, 10)"; + $expected = " WHERE `score` IN (1, 2, 10)"; $this->assertEquals($expected, $result); $result = $this->Dbo->conditions("Aro.rght = Aro.lft + 1.1"); @@ -2240,7 +2240,7 @@ class MysqlTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Dbo->conditions(array('score' => array(1, 2, 10))); - $expected = " WHERE score IN (1, 2, 10)"; + $expected = " WHERE `score` IN (1, 2, 10)"; $this->assertEquals($expected, $result); $result = $this->Dbo->conditions(array('score' => array())); @@ -2331,7 +2331,7 @@ class MysqlTest extends CakeTestCase { 'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)), 'Enrollment.yearcompleted >' => '0') ); - $this->assertRegExp('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result); + $this->assertRegExp('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(`level_of_education_id` IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result); $result = $this->Dbo->conditions(array('id <>' => '8')); $this->assertRegExp('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result); @@ -2367,7 +2367,7 @@ class MysqlTest extends CakeTestCase { $conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76)); $result = $this->Dbo->conditions($conditions); - $expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)"; + $expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76)"; $this->assertEquals($expected, $result); $conditions = array('title' => 'user(s)'); diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index de8cc903f..c38e1d085 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -1103,4 +1103,73 @@ class DboSourceTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * Test conditionKeysToString() + * + * @return void + */ + public function testConditionKeysToString() { + $Article = ClassRegistry::init('Article'); + $conn = $this->getMock('MockPDO', array('quote')); + $db = new DboTestSource; + $db->setConnection($conn); + + $conn->expects($this->at(0)) + ->method('quote') + ->will($this->returnValue('just text')); + + $conditions = array('Article.name' => 'just text'); + $result = $db->conditionKeysToString($conditions, true, $Article); + $expected = "Article.name = just text"; + $this->assertEquals($expected, $result[0]); + + $conn->expects($this->at(0)) + ->method('quote') + ->will($this->returnValue('just text')); + $conn->expects($this->at(1)) + ->method('quote') + ->will($this->returnValue('other text')); + + $conditions = array('Article.name' => array('just text', 'other text')); + $result = $db->conditionKeysToString($conditions, true, $Article); + $expected = "Article.name IN (just text, other text)"; + $this->assertEquals($expected, $result[0]); + } + +/** + * Test conditionKeysToString() with virtual field + * + * @return void + */ + public function testConditionKeysToStringVirtualField() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'extra' => 'something virtual' + ); + $conn = $this->getMock('MockPDO', array('quote')); + $db = new DboTestSource; + $db->setConnection($conn); + + $conn->expects($this->at(0)) + ->method('quote') + ->will($this->returnValue('just text')); + + $conditions = array('Article.extra' => 'just text'); + $result = $db->conditionKeysToString($conditions, true, $Article); + $expected = "(" . $Article->virtualFields['extra'] . ") = just text"; + $this->assertEquals($expected, $result[0]); + + $conn->expects($this->at(0)) + ->method('quote') + ->will($this->returnValue('just text')); + $conn->expects($this->at(1)) + ->method('quote') + ->will($this->returnValue('other text')); + + $conditions = array('Article.extra' => array('just text', 'other text')); + $result = $db->conditionKeysToString($conditions, true, $Article); + $expected = "(" . $Article->virtualFields['extra'] . ") IN (just text, other text)"; + $this->assertEquals($expected, $result[0]); + } + }