Skip to content

Commit

Permalink
Temporarily refactoring how bound values are stored to fix a bug
Browse files Browse the repository at this point in the history
Changed andWhere and orWhere so they don't need where() to be called
before
  • Loading branch information
lorenzo committed Dec 31, 2012
1 parent 6624540 commit 49fd02c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Expand Up @@ -54,14 +54,17 @@ public function add($conditions, $types = []) {
*/
public function bind($token, $value, $type) {
$param = $token;
$number = count($this->_bindings);

if (is_numeric($token)) {
$param = '?';
} else if ($param[0] !== ':') {
$param = sprintf(':c%s%s', $this->_identifier, count($this->_bindings));
$param = sprintf(':c%s%s', $this->_identifier, $number);
}

$this->_bindings[$token] = compact('value', 'type') + ['placeholder' => substr($param, 1)];
$this->_bindings[$number] = compact('value', 'type') + [
'placeholder' => substr($param, 1)
];
return $param;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Model/Datasource/Database/Query.php
Expand Up @@ -242,7 +242,7 @@ public function where($conditions = null, $types = [], $overwrite = false) {
}

public function andWhere($conditions, $types = []) {
$where = $this->_parts['where'];
$where = $this->_parts['where'] ?: new QueryExpression();

if ($where->type() === 'AND') {
$where->add($conditions, $types);
Expand All @@ -256,7 +256,7 @@ public function andWhere($conditions, $types = []) {
}

public function orWhere($conditions, $types = []) {
$where = $this->_parts['where'];
$where = $this->_parts['where'] ?: new QueryExpression([], [], 'OR');

if ($where->type() === 'OR') {
$where->add($conditions, $types);
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -448,4 +448,23 @@ public function testSelectExpressionNesting() {
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
}

/**
* Tests that Query::orWhere() can be used without calling where() before
*
* @return void
**/
public function testSelectOrWhereNoPreviousCondition() {
$this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->orWhere(['posted' => new \DateTime('2012-12-21 12:00')], ['posted' => 'datetime'])
->orWhere(['posted' => new \DateTime('2012-12-22 12:00')], ['posted' => 'datetime'])
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
}

}

0 comments on commit 49fd02c

Please sign in to comment.