Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'InvalidArgumentException' in Postgres Array's #15804

Closed
torchinsky opened this issue Feb 28, 2018 · 7 comments
Closed

'InvalidArgumentException' in Postgres Array's #15804

torchinsky opened this issue Feb 28, 2018 · 7 comments
Assignees
Labels
Milestone

Comments

@torchinsky
Copy link

First of all - sorry for my english...

What steps will reproduce the problem?

  1. Create table in postgres with two array fields.
  2. In model make rule
    [['s_all', 's_uch',], 'each', 'rule' => ['default', 'value' => null] ],
  3. In View
                <div class="row">
                    <div class="col-md-6">
                        <?= $form->field($model, 's_all[0]')->label('От')->textInput() ?>
                    </div>
                    <div class="col-md-6">
                        <?= $form->field($model, 's_all[1]')->label('До')->textInput() ?>
                    </div>
                </div>

What is the expected result?

On update expected saving data to s_all

What do you get instead?

exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in /mnt/disk/www/realty/vendor/yiisoft/yii2/db/ArrayExpression.php:189
Stack trace:
#0 /mnt/disk/www/realty/vendor/yiisoft/yii2/db/ArrayExpression.php(189): ArrayIterator->__construct(NULL)
#1 /mnt/disk/www/realty/vendor/yiisoft/yii2/validators/EachValidator.php(141): yii\db\ArrayExpression->getIterator()
#2 /mnt/disk/www/realty/vendor/yiisoft/yii2/validators/Validator.php(267): yii\validators\EachValidator->validateAttribute(Object(app\modules\realty\models\RealtyBuyers), 's_uch')
#3 /mnt/disk/www/realty/vendor/yiisoft/yii2/base/Model.php(367): yii\validators\Validator->validateAttributes(Object(app\modules\realty\models\RealtyBuyers), Array)
#4 /mnt/disk/www/realty/vendor/yiisoft/yii2/widgets/ActiveForm.php(418): yii\base\Model->validate(NULL)
#5 /mnt/disk/www/realty/modules/realty/controllers/BuyersController.php(197): yii\widgets\ActiveForm::validate(Object(app\modules\realty\models\RealtyBuyers))
#6 [internal function]: app\modules\realty\controllers\BuyersController->actionUpdate('3383')
#7 /mnt/disk/www/realty/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#8 /mnt/disk/www/realty/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#9 /mnt/disk/www/realty/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('update', Array)
#10 /mnt/disk/www/realty/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('realty/buyers/u...', Array)
#11 /mnt/disk/www/realty/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#12 /mnt/disk/www/realty/web/index.php(24): yii\base\Application->run()
#13 {main}

But if we make in view

                <div class="row">
                    <div class="col-md-6">
                        <?= $form->field($model, 's_all[0]')->label('От')->textInput() ?>
                    </div>
                    <div class="col-md-6">
                        <?= $form->field($model, 's_all[1]')->label('До')->textInput() ?>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <?= $form->field($model, 's_uch[0]')->label('От')->textInput() ?>
                    </div>
                    <div class="col-md-6">
                        <?= $form->field($model, 's_uch[1]')->label('До')->textInput() ?>
                    </div>
                </div>

it will works fine.

Additional info

Q A
Yii version 2.0.14.1
PHP version 5.6
Operating system ubuntu 16.10
@SilverFire
Copy link
Member

I think you need to change default value to []

[['s_all', 's_uch',], 'each', 'rule' => ['default', 'value' => []] ],

@SilverFire SilverFire added the status:to be verified Needs to be reproduced and validated. label Feb 28, 2018
@SilverFire SilverFire self-assigned this Feb 28, 2018
@SilverFire SilverFire added this to the 2.0.14.2 milestone Feb 28, 2018
@torchinsky
Copy link
Author

torchinsky commented Feb 28, 2018

I think you need to change default value to []
[['s_all', 's_uch',], 'each', 'rule' => ['default', 'value' => []] ],

No effect. Same error.
And it is strange idea...when we make [['s_all', 's_uch',], 'each', 'rule' => ['default', 'value' => null ] ], we expcet that each element in array by default will be NULL.
If we make [['s_all', 's_uch',], 'each', 'rule' => ['default', 'value' => [] ] ]...what this will be? Array [ [ ], [ ] ]?

Also, when we make [['s_all', 's_uch',], 'safe'], we got another error

SQLSTATE[22P02]: Invalid text representation: 7 Error: ivalid value for integer: ""
The SQL being executed was: UPDATE "realty"."buyers" SET "s_all"=ARRAY['38', '']::int2[], WHERE "id"=3383

Maybe we need some flag in config like 'onEmptySaveNull' => true?

@SilverFire
Copy link
Member

And it is strange idea...when we make [['s_all', 's_uch',], 'each', 'rule' => ['default', 'value' => null ] ], we expcet that each element in array by default will be NULL.

Enhm... yes, I was wrong. Need to check in details

@Tigrov
Copy link
Member

Tigrov commented Mar 2, 2018

@SilverFire I suppose it requires to check if value is null
This is example https://github.com/Tigrov/yii2-pgsql/blob/master/src/ColumnSchema.php#L40

@SilverFire SilverFire added type:bug Bug and removed status:to be verified Needs to be reproduced and validated. labels Mar 3, 2018
@SilverFire
Copy link
Member

@torchinsky Thank you for the report. Could you check whether your problem is fixed in dev-master?

@Tigrov yes, it was related to null handling. By the way, thank you for your yii2-pgsql extension. It was a good starting point for a me to implement arrays and JSON support in core. It also helped a lot of developers to work with arrays and JSON in PgSQL while support was not implemented in core.

@Tigrov
Copy link
Member

Tigrov commented Mar 3, 2018

@SilverFire you did a great job to implement support of array and json in right way. Thanks for it.
I will be glad if all the Postgres features are supported out of the box.

@Tigrov
Copy link
Member

Tigrov commented Mar 3, 2018

@SilverFire by the way, I think it's better to check if value is null at the first line of ColumnSchema::dbTypecast($value)

if ($value instanceof ExpressionInterface) {

To avoid any Expressions for null values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants