Fixed null values handling for PostgresSQL arrays

Fixes #15804
This commit is contained in:
SilverFire - Dmitry Naumenko
2018-03-03 18:54:33 +02:00
parent 233eeb52d7
commit d5d4b8b0f5
6 changed files with 36 additions and 3 deletions

View File

@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #15792: Added missing `yii\db\QueryBuilder::conditionClasses` setter (silverfire) - 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 #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 #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 2.0.14.1 February 24, 2018

View File

@ -182,10 +182,14 @@ class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable,
*/ */
public function getIterator() 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'); 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);
} }
} }

View File

@ -32,6 +32,9 @@ class ArrayExpressionBuilder implements ExpressionBuilderInterface
public function build(ExpressionInterface $expression, array &$params = []) public function build(ExpressionInterface $expression, array &$params = [])
{ {
$value = $expression->getValue(); $value = $expression->getValue();
if ($value === null) {
return 'NULL';
}
if ($value instanceof Query) { if ($value instanceof Query) {
list ($sql, $params) = $this->queryBuilder->build($value, $params); list ($sql, $params) = $this->queryBuilder->build($value, $params);

View File

@ -102,6 +102,8 @@ class ArrayParser
if (!$isQuoted && $result === 'NULL') { if (!$isQuoted && $result === 'NULL') {
$result = null; $result = null;
} elseif ($isQuoted && $result === '{}') {
$result = [];
} }
return $result; return $result;

View File

@ -83,6 +83,8 @@ class ColumnSchema extends \yii\db\ColumnSchema
array_walk_recursive($value, function (&$val, $key) { array_walk_recursive($value, function (&$val, $key) {
$val = $this->phpTypecastValue($val); $val = $this->phpTypecastValue($val);
}); });
} elseif ($value === null) {
return null;
} }
return $this->deserializeArrayColumnToArrayExpression return $this->deserializeArrayColumnToArrayExpression

View File

@ -228,6 +228,27 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest
'jsonb_col' => [[null, 'a', 'b', '\"', '{"af"}']], 'jsonb_col' => [[null, 'a', 'b', '\"', '{"af"}']],
'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], 'json')], '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' => [[ 'arrays packed in classes' => [[
'intarray_col' => [ 'intarray_col' => [
new ArrayExpression([1,-2,null,'42'], 'int', 1), new ArrayExpression([1,-2,null,'42'], 'int', 1),
@ -257,7 +278,7 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest
'jsonb_col' => [ 'jsonb_col' => [
pi() pi()
], ],
]] ]],
]; ];
} }
} }