Skip to content

Commit 49fd02c

Browse files
committed
Temporarily refactoring how bound values are stored to fix a bug
Changed andWhere and orWhere so they don't need where() to be called before
1 parent 6624540 commit 49fd02c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/Cake/Model/Datasource/Database/Expression/QueryExpression.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ public function add($conditions, $types = []) {
5454
*/
5555
public function bind($token, $value, $type) {
5656
$param = $token;
57+
$number = count($this->_bindings);
5758

5859
if (is_numeric($token)) {
5960
$param = '?';
6061
} else if ($param[0] !== ':') {
61-
$param = sprintf(':c%s%s', $this->_identifier, count($this->_bindings));
62+
$param = sprintf(':c%s%s', $this->_identifier, $number);
6263
}
6364

64-
$this->_bindings[$token] = compact('value', 'type') + ['placeholder' => substr($param, 1)];
65+
$this->_bindings[$number] = compact('value', 'type') + [
66+
'placeholder' => substr($param, 1)
67+
];
6568
return $param;
6669
}
6770

lib/Cake/Model/Datasource/Database/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public function where($conditions = null, $types = [], $overwrite = false) {
242242
}
243243

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

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

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

261261
if ($where->type() === 'OR') {
262262
$where->add($conditions, $types);

lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,23 @@ public function testSelectExpressionNesting() {
448448
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
449449
}
450450

451+
/**
452+
* Tests that Query::orWhere() can be used without calling where() before
453+
*
454+
* @return void
455+
**/
456+
public function testSelectOrWhereNoPreviousCondition() {
457+
$this->_insertDateRecords();
458+
$query = new Query($this->connection);
459+
$result = $query
460+
->select(['id'])
461+
->from('dates')
462+
->orWhere(['posted' => new \DateTime('2012-12-21 12:00')], ['posted' => 'datetime'])
463+
->orWhere(['posted' => new \DateTime('2012-12-22 12:00')], ['posted' => 'datetime'])
464+
->execute();
465+
$this->assertCount(2, $result);
466+
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
467+
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
468+
}
469+
451470
}

0 commit comments

Comments
 (0)