Skip to content

Commit

Permalink
Adding not() method
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 1, 2013
1 parent 5c93dcf commit 4f1028f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Expand Up @@ -112,6 +112,10 @@ public function or_($conditions, $types = []) {
return new self($conditions, $types, 'OR');
}

public function not($conditions, $types = []) {
return $this->add(['NOT' => $conditions], $types);
}

/**
* Associates a query placeholder to a value and a type for next execution
*
Expand Down
27 changes: 27 additions & 0 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -811,11 +811,38 @@ public function testSelectExpressionComposition() {

/**
* Tests that conditions can be nested with an unary operator using the array notation
* and the not() method
*
* @return void
**/
public function testSelectWhereNot() {
$this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
return $exp->not(
$exp->and_(['id' => 2, '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' => 3], $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
return $exp->not(
$exp->and_(['id' => 2, 'posted' => new \DateTime('2012-12-21 12:00')], ['posted' => 'datetime'])
);
})
->execute();
$this->assertCount(3, $result);

$query = new Query($this->connection);
$result = $query
->select(['id'])
Expand Down

0 comments on commit 4f1028f

Please sign in to comment.