Skip to content

Commit

Permalink
No implicit IN condition operator for array value
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 9, 2023
1 parent 856f742 commit f292d95
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ name=John OR surname=Smith.
If you are building multiple conditions against the same field, you can use this
format::

$m->addCondition('name', ['John', 'Joe']);
$m->addCondition('name', 'in', ['John', 'Joe']);

For all other cases you can implement them with :php:meth:`Model::expr`::

Expand Down Expand Up @@ -349,7 +349,7 @@ Creates condition object based on provided arguments. It acts similar to Model::

$key can be Model field name, Field object, Expression object, FALSE (interpreted as Expression('false')), TRUE (interpreted as empty condition) or an array in the form of [$key, $operator, $value]
$operator can be one of the supported operators >, <, >=, <=, !=, in, not in, like, not like, regexp, not regexp
$value can be Field object, Expression object, array (interpreted as 'any of the values') or other scalar value
$value can be Field object, Expression object or any scalar value

If $value is omitted as argument then $operator is considered as $value and '=' is used as operator

Expand Down
7 changes: 1 addition & 6 deletions src/Model/Scope/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,7 @@ public function __construct($key, $operator = null, $value = null)
->addMoreInfo('value', $value);
}

if (!in_array($this->operator, [
self::OPERATOR_EQUALS,
self::OPERATOR_IN,
self::OPERATOR_DOESNOT_EQUAL,
self::OPERATOR_NOT_IN,
], true)) {
if (!in_array($this->operator, [self::OPERATOR_IN, self::OPERATOR_NOT_IN], true)) {
throw (new Exception('Operator is not supported for array condition value'))
->addMoreInfo('operator', $operator)
->addMoreInfo('value', $value);
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Array_/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function evaluateIf($v1, string $operator, $v2): bool

switch (strtoupper($operator)) {
case '=':
$result = is_array($v2) ? $this->evaluateIf($v1, 'IN', $v2) : $v1 === $v2;
$result = $v1 === $v2;

break;
case '>':
Expand Down
4 changes: 1 addition & 3 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ protected function _subrenderCondition(array $row): string
$value = $value->getDsqlExpression($this);
}

if (is_array($value)) {
$cond = 'in';
} elseif ($value instanceof self && $value->mode === 'select') {
if ($value instanceof self && $value->mode === 'select') {
$cond = 'in';
} else {
$cond = '=';
Expand Down
25 changes: 23 additions & 2 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function testArrayCondition(): void

$m = new Model($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', ['John', 'Doe']);
$m->addCondition('name', 'in', ['John', 'Doe']);
static::assertCount(1, $m->export());

$m = new Model($this->db, ['table' => 'user']);
Expand All @@ -308,7 +308,7 @@ public function testArrayCondition(): void

$m = new Model($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', []); // this should not fail, should be always false
$m->addCondition('name', 'in', []); // this should not fail, should be always false
static::assertCount(0, $m->export());

$m = new Model($this->db, ['table' => 'user']);
Expand All @@ -317,6 +317,27 @@ public function testArrayCondition(): void
static::assertCount(3, $m->export());
}

public function testConditionEqualWithArrayException(): void
{
$m = new Model($this->db, ['table' => 'user']);
$m->addField('name');

$this->expectException(Exception::class);
$this->expectExceptionMessage('Operator is not supported for array condition value');
$m->addCondition('name', ['John', 'Doe']);
}

public function testConditionInWithNonArrayException(): void
{
$m = new Model($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', 'not in', 'John');

$this->expectException(Exception::class);
$this->expectExceptionMessage('Unsupported operator for non-array value');
$m->export();
}

public function testDateCondition(): void
{
$this->setDb([
Expand Down
10 changes: 1 addition & 9 deletions tests/Persistence/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public function testConditionRegexp(): void
unset($result);

$m->scope()->clear();
$m->addCondition('code', [11, 12]);
$m->addCondition('code', 'IN', [11, 12]);
$result = $m->action('select')->getRows();
static::assertCount(2, $result);
static::assertSame($dbDataCountries[1], $result[1]);
Expand All @@ -477,14 +477,6 @@ public function testConditionRegexp(): void
static::assertSame($dbDataCountries[8], $result[8]);
static::assertSame($dbDataCountries[9], $result[9]);
unset($result);

$m->scope()->clear();
$m->addCondition('code', '!=', [11, 12, 13, 14, 15, 16, 17]);
$result = $m->action('select')->getRows();
static::assertCount(2, $result);
static::assertSame($dbDataCountries[8], $result[8]);
static::assertSame($dbDataCountries[9], $result[9]);
unset($result);
}

public function testAggregates(): void
Expand Down
9 changes: 9 additions & 0 deletions tests/Persistence/Sql/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ public function testWhereIncompatibleObject3(): void
$this->q('[where]')->where('a', '!=', new \DateTime());
}

public function testWhereNoOperatorWithArrayException(): void
{
$q = $this->q('[where]')->where('a', [1, 2]);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Unsupported operator for array value');
$q->render();
}

/**
* @param mixed $value
*
Expand Down

0 comments on commit f292d95

Please sign in to comment.