Skip to content

Commit

Permalink
Fix condition parsing in mysql specific cases.
Browse files Browse the repository at this point in the history
When using collation + array values the incorrect operator would be
used. IN should be used over =.

Fixes #3772
  • Loading branch information
markstory committed Apr 19, 2013
1 parent 3680ee0 commit 0f3d28c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -2588,7 +2588,7 @@ protected function _parseKey($model, $key, $value) {
}

if (!preg_match($operatorMatch, trim($operator))) {
$operator .= ' =';
$operator .= is_array($value) ? ' IN' : ' =';
}
$operator = trim($operator);

Expand Down
22 changes: 22 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -2446,6 +2446,11 @@ public function testArrayConditionsParsing() {
$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)'";
Expand Down Expand Up @@ -2499,6 +2504,23 @@ public function testArrayConditionsParsing() {
$this->assertEquals($expected, $result);
}

/**
* 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
*
Expand Down

0 comments on commit 0f3d28c

Please sign in to comment.