Skip to content

Commit 0f3d28c

Browse files
committed
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
1 parent 3680ee0 commit 0f3d28c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Cake/Model/Datasource/DboSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ protected function _parseKey($model, $key, $value) {
25882588
}
25892589

25902590
if (!preg_match($operatorMatch, trim($operator))) {
2591-
$operator .= ' =';
2591+
$operator .= is_array($value) ? ' IN' : ' =';
25922592
}
25932593
$operator = trim($operator);
25942594

lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,11 @@ public function testArrayConditionsParsing() {
24462446
$expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
24472447
$this->assertEquals($expected, $result);
24482448

2449+
$conditions = array('`Correction`.`source` collate utf8_bin' => array('kiwi', 'pear'));
2450+
$result = $this->Dbo->conditions($conditions);
2451+
$expected = " WHERE `Correction`.`source` collate utf8_bin IN ('kiwi', 'pear')";
2452+
$this->assertEquals($expected, $result);
2453+
24492454
$conditions = array('title' => 'user(s)');
24502455
$result = $this->Dbo->conditions($conditions);
24512456
$expected = " WHERE `title` = 'user(s)'";
@@ -2499,6 +2504,23 @@ public function testArrayConditionsParsing() {
24992504
$this->assertEquals($expected, $result);
25002505
}
25012506

2507+
/**
2508+
* Test that array conditions with only one element work.
2509+
*
2510+
* @return
2511+
*/
2512+
public function testArrayConditionsOneElement() {
2513+
$conditions = array('id' => array(1));
2514+
$result = $this->Dbo->conditions($conditions);
2515+
$expected = " WHERE id = (1)";
2516+
$this->assertEquals($expected, $result);
2517+
2518+
$conditions = array('id NOT' => array(1));
2519+
$result = $this->Dbo->conditions($conditions);
2520+
$expected = " WHERE NOT (id = (1))";
2521+
$this->assertEquals($expected, $result);
2522+
}
2523+
25022524
/**
25032525
* testArrayConditionsParsingComplexKeys method
25042526
*

0 commit comments

Comments
 (0)