Skip to content

Commit

Permalink
Fixed null values handling for PostgresSQL arrays
Browse files Browse the repository at this point in the history
Fixes #15804
  • Loading branch information
SilverFire committed Mar 3, 2018
1 parent 233eeb5 commit d5d4b8b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #15792: Added missing `yii\db\QueryBuilder::conditionClasses` setter (silverfire)
- Bug #15822: Fixed `yii\base\Component::off()` not to throw an exception when handler does not exist (silverfire)
- Bug #15817: Fixed support of deprecated array format type casting in `yii\db\Command::bindValues()` (silverfire)
- Bug #15804: Fixed `null` values handling for PostgresSQL arrays (silverfire)


2.0.14.1 February 24, 2018
Expand Down
8 changes: 6 additions & 2 deletions framework/db/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ public function count()
*/
public function getIterator()
{
if ($this->getValue() instanceof QueryInterface) {
$value = $this->getValue();
if ($value instanceof QueryInterface) {
throw new InvalidConfigException('The ArrayExpression class can not be iterated when the value is a QueryInterface object');
}
if ($value === null) {
$value = [];
}

return new \ArrayIterator($this->getValue());
return new \ArrayIterator($value);
}
}
3 changes: 3 additions & 0 deletions framework/db/pgsql/ArrayExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ArrayExpressionBuilder implements ExpressionBuilderInterface
public function build(ExpressionInterface $expression, array &$params = [])
{
$value = $expression->getValue();
if ($value === null) {
return 'NULL';
}

if ($value instanceof Query) {
list ($sql, $params) = $this->queryBuilder->build($value, $params);
Expand Down
2 changes: 2 additions & 0 deletions framework/db/pgsql/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ private function parseString($value, &$i)

if (!$isQuoted && $result === 'NULL') {
$result = null;
} elseif ($isQuoted && $result === '{}') {
$result = [];

This comment has been minimized.

Copy link
@Tigrov

Tigrov Mar 3, 2018

Member

What about if array of strings contain a string value "{}".
Test it $model->value = ['{}']; the result from DB must be the same.

This comment has been minimized.

Copy link
@SilverFire

SilverFire Mar 3, 2018

Author Member

This was fixed by next commit 5fa25b3

This comment has been minimized.

Copy link
@Tigrov

Tigrov Mar 3, 2018

Member

Ok, sorry didn't see it.

}

return $result;
Expand Down
2 changes: 2 additions & 0 deletions framework/db/pgsql/ColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function phpTypecast($value)
array_walk_recursive($value, function (&$val, $key) {
$val = $this->phpTypecastValue($val);
});
} elseif ($value === null) {
return null;
}

return $this->deserializeArrayColumnToArrayExpression
Expand Down
23 changes: 22 additions & 1 deletion tests/framework/db/pgsql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ public function arrayValuesProvider()
'jsonb_col' => [[null, 'a', 'b', '\"', '{"af"}']],
'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], 'json')],
]],
'null arrays values' => [[
'intarray_col' => [
null,
],
'textarray2_col' => [
[null, null],
new ArrayExpression([null, null], 'text', 2),
],
'json_col' => [
null
],
'jsonarray_col' => [
null
],
]],
'empty arrays values' => [[
'textarray2_col' => [
['', ''],
new ArrayExpression([[], []], 'text', 2),
],
]],
'arrays packed in classes' => [[
'intarray_col' => [
new ArrayExpression([1,-2,null,'42'], 'int', 1),
Expand Down Expand Up @@ -257,7 +278,7 @@ public function arrayValuesProvider()
'jsonb_col' => [
pi()
],
]]
]],
];
}
}
Expand Down

0 comments on commit d5d4b8b

Please sign in to comment.