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

Use Param object instead of array #718

Closed
Tigrov opened this issue May 19, 2023 · 1 comment · Fixed by #722
Closed

Use Param object instead of array #718

Tigrov opened this issue May 19, 2023 · 1 comment · Fixed by #722
Labels

Comments

@Tigrov
Copy link
Member

Tigrov commented May 19, 2023

https://github.com/yiisoft/db/blob/master/src/Schema/AbstractColumnSchema.php#L274

if (
    is_array($value)
    && count($value) === 2
    && isset($value[1])
    && in_array($value[1], $this->getDataTypes(), true)
) {
    return new Param((string) $value[0], $value[1]);
}

Remove this code and use directly new Param($value, $dataType)

To avoid problems with support values of array fields, eg:

$value = [$val1, $val2]; // where $val2 in array $this->getDataTypes()
// or
$value = ['key' => $val1, 1 => $val2];

The problem when you use array containing the value as the first element and the PDO type as the second you can get unexpected result for array fields, e.g.

$db->createCommand()->createTable('{{%table}}', ['array' => 'json'])->execute(); // or array type instead of json ('integer []')
$db->createCommand()->insert('{{%table}}', ['array' => [1, 2]])->execute();
$result = $db->createCommand('SELECT * FROM {{%table}}')->queryScalar();
// expected $result === '[1, 2]'
@vjik vjik added the type:bug Bug label May 23, 2023
@samdark samdark modified the milestone: 1.1.0 Jun 4, 2023
@Tigrov
Copy link
Member Author

Tigrov commented Jun 5, 2023

I found the array syntax to declare PDO parameter type was marked as deprecated.

https://github.com/yiisoft/yii2/blob/master/framework/UPGRADE.md?plain=1#L497

  • yii\db\PdoValue class has been introduced to replace a special syntax that was used to declare PDO parameter type
    when binding parameters to an SQL command, for example: ['value', \PDO::PARAM_STR].
    You should use new PdoValue('value', \PDO::PARAM_STR) instead. Old syntax will be removed in Yii 2.1.

@terabytesoftw terabytesoftw linked a pull request Jun 17, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants