diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 2744f1f08..ac451201d 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1852,10 +1852,19 @@ class DboSource extends DataSource { * @access private */ function __parseKey($model, $key, $value) { + $operatorMatch = '/^((' . join(')|(', $this->__sqlOps); + $operatorMatch .= '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is'; + if (!strpos($key, ' ')) { $operator = '='; } else { + $key = trim($key); list($key, $operator) = explode(' ', $key, 2); + + if (!preg_match($operatorMatch, trim($operator))) { + $key = $key . ' ' . $operator; + list($key, $operator) = str_split($key, strrpos($key, ' ')); + } } $type = (is_object($model) ? $model->getColumnType($key) : null); $null = ($value === null || (is_array($value) && empty($value))); @@ -1864,7 +1873,7 @@ class DboSource extends DataSource { $data = $this->conditionKeysToString(array($operator => array($key => $value)), true, $model); return $data[0]; } - if (!preg_match('/^((' . join(')|(', $this->__sqlOps) . '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is', trim($operator))) { + if (!preg_match($operatorMatch, trim($operator))) { $operator .= ' ='; } 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 4077cf577..67cd2b059 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -91,7 +91,7 @@ class TestModel extends CakeTestModel { * @return void */ function find($conditions = null, $fields = null, $order = null, $recursive = null) { - return $conditions; + return array($conditions, $fields); } /** * findAll method @@ -2524,7 +2524,7 @@ class DboSourceTest extends CakeTestCase { $result = $this->testDb->conditions(array( '(Stats.clicks * 100) / Stats.views >' => 50 )); - $expected = " WHERE (`Stats.clicks` * 100) / `Stats`.`views` > 50"; + $expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50"; $this->assertEqual($result, $expected); } /** @@ -3057,27 +3057,47 @@ class DboSourceTest extends CakeTestCase { */ function testMagicMethodQuerying() { $result = $this->testDb->query('findByFieldName', array('value'), $this->Model); - $expected = array('TestModel.field_name' => 'value'); + $expected = array('first', array( + 'conditions' => array('TestModel.field_name' => 'value'), + 'fields' => null, 'order' => null, 'recursive' => null + )); $this->assertEqual($result, $expected); $result = $this->testDb->query('findAllByFieldName', array('value'), $this->Model); - $expected = array('TestModel.field_name' => 'value'); + $expected = array('all', array( + 'conditions' => array('TestModel.field_name' => 'value'), + 'fields' => null, 'order' => null, 'limit' => null, + 'page' => null, 'recursive' => null + )); $this->assertEqual($result, $expected); $result = $this->testDb->query('findAllById', array('a'), $this->Model); - $expected = array('TestModel.id' => 'a'); + $expected = array('all', array( + 'conditions' => array('TestModel.id' => 'a'), + 'fields' => null, 'order' => null, 'limit' => null, + 'page' => null, 'recursive' => null + )); $this->assertEqual($result, $expected); $result = $this->testDb->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model); - $expected = array('TestModel.field_name' => array('value1', 'value2', 'value3')); + $expected = array('first', array( + 'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')), + 'fields' => null, 'order' => null, 'recursive' => null + )); $this->assertEqual($result, $expected); $result = $this->testDb->query('findByFieldName', array(null), $this->Model); - $expected = array('TestModel.field_name' => null); + $expected = array('first', array( + 'conditions' => array('TestModel.field_name' => null), + 'fields' => null, 'order' => null, 'recursive' => null + )); $this->assertEqual($result, $expected); $result = $this->testDb->query('findByFieldName', array('= a'), $this->Model); - $expected = array('TestModel.field_name' => '= a'); + $expected = array('first', array( + 'conditions' => array('TestModel.field_name' => '= a'), + 'fields' => null, 'order' => null, 'recursive' => null + )); $this->assertEqual($result, $expected); $result = $this->testDb->query('findByFieldName', array(), $this->Model); @@ -3475,8 +3495,9 @@ class DboSourceTest extends CakeTestCase { * @return void */ function testReconnect() { - $this->testDb->reconnect(); + $this->testDb->reconnect(array('prefix' => 'foo')); $this->assertTrue($this->testDb->connected); + $this->assertEqual($this->testDb->config['prefix'], 'foo'); } /** * testRealQueries method