Skip to content

Commit

Permalink
Correctly implementing support for NOT operator
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 1, 2013
1 parent a0206f6 commit 5c93dcf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Expand Up @@ -179,7 +179,7 @@ public function traverse($callable) {
}

protected function _addConditions(array $conditions, array $types) {
$operators = array('and', 'or', 'not', 'xor');
$operators = array('and', 'or', 'xor');

foreach ($conditions as $k => $c) {
$numericKey = is_numeric($k);
Expand All @@ -198,6 +198,11 @@ protected function _addConditions(array $conditions, array $types) {
continue;
}

if (strtolower($k) === 'not') {
$this->_conditions[] = new UnaryExpression(new self($c, $types), [], 'NOT');
continue;
}

if ($c instanceof QueryExpression && count($c) > 0) {
$this->_conditions[] = $c;
continue;
Expand Down
11 changes: 11 additions & 0 deletions lib/Cake/Model/Datasource/Database/Expression/UnaryExpression.php
@@ -0,0 +1,11 @@
<?php

namespace Cake\Model\Datasource\Database\Expression;

class UnaryExpression extends QueryExpression {

public function sql() {
return $this->_conjunction . ' (' . ((string)current($this->_conditions)) . ')';
}

}
21 changes: 21 additions & 0 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -808,4 +808,25 @@ public function testSelectExpressionComposition() {
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
}

/**
* Tests that conditions can be nested with an unary operator using the array notation
*
* @return void
**/
public function testSelectWhereNot() {
$this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where([
'not' => ['or' => ['id' => 1, 'id >' => 2], 'id' => 3]
])
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
}

}

0 comments on commit 5c93dcf

Please sign in to comment.