Skip to content

Commit

Permalink
Fixes yiisoft#16081: Fixed composite IN using just one column
Browse files Browse the repository at this point in the history
  • Loading branch information
rugabarbo authored and samdark committed Dec 31, 2018
1 parent ebb5976 commit b128ec8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.16 under development
------------------------

- Bug #16081: Fixed composite IN using just one column (rugabarbo)
- Bug #16926: Fix shell autocompletion (GHopperMSK)
- Bug #15850: check basePath is writable on publish in AssetManager (Groonya)
- Bug #16910: Fix messages sorting on extract (Groonya)
Expand Down
27 changes: 23 additions & 4 deletions framework/db/conditions/InConditionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,22 @@ public function build(ExpressionInterface $expression, array &$params = [])
// ensure values is an array
$values = (array) $values;
}
if ($column instanceof \Traversable || ((is_array($column) || $column instanceof \Countable) && count($column) > 1)) {
return $this->buildCompositeInCondition($operator, $column, $values, $params);
}

if (is_array($column)) {
$column = reset($column);
if (count($column) > 1) {
return $this->buildCompositeInCondition($operator, $column, $values, $params);
} else {
$column = reset($column);
}
}

if ($column instanceof \Traversable) {
if (iterator_count($column) > 1) {
return $this->buildCompositeInCondition($operator, $column, $values, $params);
} else {
$column->rewind();
$column = $column->current();
}
}

$sqlValues = $this->buildValues($expression, $values, $params);
Expand Down Expand Up @@ -88,6 +98,15 @@ protected function buildValues(ConditionInterface $condition, $values, &$params)
$sqlValues = [];
$column = $condition->getColumn();

if (is_array($column)) {
$column = reset($column);
}

if ($column instanceof \Traversable) {
$column->rewind();
$column = $column->current();
}

foreach ($values as $i => $value) {
if (is_array($value) || $value instanceof \ArrayAccess) {
$value = isset($value[$column]) ? $value[$column] : null;
Expand Down
13 changes: 13 additions & 0 deletions tests/framework/db/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,19 @@ public function conditionProvider()
'([[id]], [[name]]) IN ((:qp0, :qp1))',
[':qp0' => 1, ':qp1' => 'oy'],
],
'composite in (just one column)' => [
['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]],
'[[id]] IN (:qp0, :qp1)',
[':qp0' => 1, ':qp1' => 2],
],
'composite in using array objects (just one column)' => [
['in', new TraversableObject(['id']), new TraversableObject([
['id' => 1, 'name' => 'Name1'],
['id' => 2, 'name' => 'Name2'],
])],
'[[id]] IN (:qp0, :qp1)',
[':qp0' => 1, ':qp1' => 2],
],

// in using array objects.
[['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
Expand Down

0 comments on commit b128ec8

Please sign in to comment.