Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Automatically casting to array values that are passed using the "in" or

"not in" operators, fixes #2702
  • Loading branch information...
commit d90ea72b731418eca7e71fed74c87629e6fbc1bd 1 parent 82fe8ce
@lorenzo lorenzo authored
View
33 src/Database/Expression/QueryExpression.php
@@ -109,7 +109,7 @@ public function add($conditions, $types = []) {
return $this;
}
- if ($conditions instanceof self && count($conditions) > 0) {
+ if ($conditions instanceof ExpressionInterface) {
$this->_conditions[] = $conditions;
return $this;
}
@@ -129,7 +129,7 @@ public function add($conditions, $types = []) {
* @return QueryExpression
*/
public function eq($field, $value, $type = null) {
- return $this->add([$field => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, '='));
}
/**
@@ -143,7 +143,7 @@ public function eq($field, $value, $type = null) {
* @return QueryExpression
*/
public function notEq($field, $value, $type = null) {
- return $this->add([$field . ' !=' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, '!='));
}
/**
@@ -155,7 +155,7 @@ public function notEq($field, $value, $type = null) {
* @return QueryExpression
*/
public function gt($field, $value, $type = null) {
- return $this->add([$field . ' >' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, '>'));
}
/**
@@ -167,7 +167,7 @@ public function gt($field, $value, $type = null) {
* @return QueryExpression
*/
public function lt($field, $value, $type = null) {
- return $this->add([$field . ' <' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, '<'));
}
/**
@@ -179,7 +179,7 @@ public function lt($field, $value, $type = null) {
* @return QueryExpression
*/
public function gte($field, $value, $type = null) {
- return $this->add([$field . ' >=' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, '>='));
}
/**
@@ -191,7 +191,7 @@ public function gte($field, $value, $type = null) {
* @return QueryExpression
*/
public function lte($field, $value, $type = null) {
- return $this->add([$field . ' <=' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, '<='));
}
/**
@@ -223,7 +223,7 @@ public function isNotNull($field) {
* @return QueryExpression
*/
public function like($field, $value, $type = null) {
- return $this->add([$field . ' LIKE' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, 'LIKE'));
}
/**
@@ -235,7 +235,7 @@ public function like($field, $value, $type = null) {
* @return QueryExpression
*/
public function notLike($field, $value, $type = null) {
- return $this->add([$field . ' NOT LIKE' => $value], $type ? [$field => $type] : []);
+ return $this->add(new Comparison($field, $value, $type, 'NOT LIKE'));
}
/**
@@ -248,7 +248,10 @@ public function notLike($field, $value, $type = null) {
* @return QueryExpression
*/
public function in($field, $values, $type = null) {
- return $this->add([$field . ' IN' => $values], $type ? [$field => $type] : []);
+ $type = $type ?: 'string';
+ $type .= '[]';
@markstory Owner

Is this syntax something we should publically document? It is not in the docs right now.

@lorenzo Owner
lorenzo added a note

Definitely, we should documented. We don't don't mention much about the type system yet, I think we should expand on that area too

@markstory Owner

I will add that to my list of docs changes to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $values = $values instanceof ExpressionInterface ? $values : (array)$values;
+ return $this->add(new Comparison($field, $values, $type, 'IN'));
}
/**
@@ -261,7 +264,10 @@ public function in($field, $values, $type = null) {
* @return QueryExpression
*/
public function notIn($field, $values, $type = null) {
- return $this->add([$field . ' NOT IN' => $values], $type ? [$field => $type] : []);
+ $type = $type ?: 'string';
+ $type .= '[]';
+ $values = $values instanceof ExpressionInterface ? $values : (array)$values;
+ return $this->add(new Comparison($field, $values, $type, 'NOT IN'));
}
// @codingStandardsIgnoreStart
@@ -472,6 +478,11 @@ protected function _parseCondition($field, $value, $types) {
$type .= $typeMultiple ? null : '[]';
$operator = $operator == '=' ? 'IN' : $operator;
$operator = $operator == '!=' ? 'NOT IN' : $operator;
+ $typeMultiple = true;
+ }
+
+ if ($typeMultiple) {
+ $value = $value instanceof ExpressionInterface ? $value : (array)$value;
}
return new Comparison($expression, $value, $type, $operator);
View
86 tests/TestCase/Database/QueryTest.php
@@ -846,6 +846,92 @@ public function testSelectWhereOperatorMethods() {
}
/**
+ * Tests that calling "in" and "notIn" will cast the passed values to an array
+ *
+ * @return void
+ */
+ public function testInValueCast() {
+ $query = new Query($this->connection);
+ $result = $query
+ ->select(['id'])
+ ->from('comments')
+ ->where(function($exp) {
+ return $exp->in('created', '2007-03-18 10:45:23', 'datetime');
+ })
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertEquals(['id' => 1], $result->fetch('assoc'));
+
+
+ $query = new Query($this->connection);
+ $result = $query
+ ->select(['id'])
+ ->from('comments')
+ ->where(function($exp) {
+ return $exp->notIn('created', '2007-03-18 10:45:23', 'datetime');
+ })
+ ->execute();
+ $this->assertCount(5, $result);
+ $this->assertEquals(['id' => 2], $result->fetch('assoc'));
+ $this->assertEquals(['id' => 3], $result->fetch('assoc'));
+ $this->assertEquals(['id' => 4], $result->fetch('assoc'));
+ $this->assertEquals(['id' => 5], $result->fetch('assoc'));
+
+ $query = new Query($this->connection);
+ $result = $query
+ ->select(['id'])
+ ->from('comments')
+ ->where(function($exp, $q) {
+ return $exp->in(
+ 'created',
+ $q->newExpr()->add("'2007-03-18 10:45:23'"),
+ 'datetime'
+ );
+ })
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertEquals(['id' => 1], $result->fetch('assoc'));
+
+ $query = new Query($this->connection);
+ $result = $query
+ ->select(['id'])
+ ->from('comments')
+ ->where(function($exp, $q) {
+ return $exp->notIn(
+ 'created',
+ $q->newExpr()->add("'2007-03-18 10:45:23'"),
+ 'datetime'
+ );
+ })
+ ->execute();
+ $this->assertCount(5, $result);
+ }
+
+/**
+ * Tests that calling "in" and "notIn" will cast the passed values to an array
+ *
+ * @return void
+ */
+ public function testInValueCast2() {
+ $query = new Query($this->connection);
+ $result = $query
+ ->select(['id'])
+ ->from('comments')
+ ->where(['created IN' => '2007-03-18 10:45:23'])
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertEquals(['id' => 1], $result->fetch('assoc'));
+
+ $query = new Query($this->connection);
+ $result = $query
+ ->select(['id'])
+ ->from('comments')
+ ->where(['created NOT IN' => '2007-03-18 10:45:23'])
+ ->execute();
+ $this->assertCount(5, $result);
+ }
+
+/**
* Tests nesting query expressions both using arrays and closures
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.