Browse Source

Fix condition parsing in mysql specific cases.

When using collation + array values the incorrect operator would be
used. IN should be used over =.

Fixes #3772
mark_story 13 years ago
parent
commit
0f3d28c6ea

+ 1 - 1
lib/Cake/Model/Datasource/DboSource.php

@@ -2588,7 +2588,7 @@ class DboSource extends DataSource {
 		}
 
 		if (!preg_match($operatorMatch, trim($operator))) {
-			$operator .= ' =';
+			$operator .= is_array($value) ? ' IN' :  ' =';
 		}
 		$operator = trim($operator);
 

+ 22 - 0
lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php

@@ -2446,6 +2446,11 @@ class MysqlTest extends CakeTestCase {
 		$expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
 		$this->assertEquals($expected, $result);
 
+		$conditions = array('`Correction`.`source` collate utf8_bin' => array('kiwi', 'pear'));
+		$result = $this->Dbo->conditions($conditions);
+		$expected = " WHERE `Correction`.`source` collate utf8_bin IN ('kiwi', 'pear')";
+		$this->assertEquals($expected, $result);
+
 		$conditions = array('title' => 'user(s)');
 		$result = $this->Dbo->conditions($conditions);
 		$expected = " WHERE `title` = 'user(s)'";
@@ -2500,6 +2505,23 @@ class MysqlTest extends CakeTestCase {
 	}
 
 /**
+ * Test that array conditions with only one element work.
+ *
+ * @return
+ */
+	public function testArrayConditionsOneElement() {
+		$conditions = array('id' => array(1));
+		$result = $this->Dbo->conditions($conditions);
+		$expected = " WHERE id = (1)";
+		$this->assertEquals($expected, $result);
+
+		$conditions = array('id NOT' => array(1));
+		$result = $this->Dbo->conditions($conditions);
+		$expected = " WHERE NOT (id = (1))";
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * testArrayConditionsParsingComplexKeys method
  *
  * @return void