Skip to content
Permalink
Browse files

Adding support for closures in orWhere()

  • Loading branch information...
lorenzo committed Jan 1, 2013
1 parent 2ce8f98 commit a99c5ae6effcac431654eb7243aabe38e0e23faa
@@ -267,6 +267,10 @@ public function andWhere($conditions, $types = []) {
public function orWhere($conditions, $types = []) {
$where = $this->_parts['where'] ?: new QueryExpression([], [], 'OR');
if (is_callable($conditions)) {
$conditions = $conditions(new QueryExpression, $this);
}
if ($where->type() === 'OR') {
$where->add($conditions, $types);
} else {
@@ -562,4 +562,40 @@ public function testSelectAndWhereUsingClosure() {
$this->assertCount(0, $result);
}
/**
* Tests that it is possible to pass a closure to orWhere() to build a set of
* conditions and return the expression to be used
*
* @return void
**/
public function testSelectOrWhereUsingClosure() {
$this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(['id' => '1'])
->orWhere(function($exp) {
return $exp->equals('posted', new \DateTime('2012-12-22 12:00'), 'datetime');
})
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(['id' => '1'])
->orWhere(function($exp) {
return $exp
->equals('posted', new \DateTime('2012-12-22 12:00'), 'datetime')
->equals('id', 3);
})
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
}
}

0 comments on commit a99c5ae

Please sign in to comment.
You can’t perform that action at this time.