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

Incorrect parsing PostgreSQL response for "ARRAY" columns #20250

Open
abolotin opened this issue Sep 2, 2024 · 1 comment
Open

Incorrect parsing PostgreSQL response for "ARRAY" columns #20250

abolotin opened this issue Sep 2, 2024 · 1 comment

Comments

@abolotin
Copy link

abolotin commented Sep 2, 2024

What steps will reproduce the problem?

Creating table:
CREATE TABLE test (data VARCHAR(255)[]);

Filling up:
INSERT INTO test (data) VALUES (ARRAY['qwe', 'asd']);

Creating simple model fro this table, named "Test":

namespace common\models;
class Test extends \yii\db\ActiveRecord {
    public static function tableName() {
        return 'test';
    }
}

Selecting one item of array:

$model=Test::find()->select([new \yii\db\Expression('data[1]')])->one();
print_r($row->toArray());

What is the expected result?

Array
(
    [data] => Array
        (
            [0] => qwe
        )
)

What do you get instead?

Array
(
    [data] => Array
        (
            [0] => we
        )
)

First symbol lost.

Additional info

Q A
Yii version 2.0.49.3
PHP version 8.2.20
Operating system Debian

Problen in yii\db\pgsql\ArrayParser, method parseArray() (line 51) always skips first symbol, because expecting it to be equal to "{". But for "data[1]" query result contains only item value "qwe", not "{qwe}".

Possible solution:
replace for (++$i; $i < $len; ++$i) with for ($i; $i < $len; ++$i) (line 55)

@Tigrov
Copy link
Member

Tigrov commented Oct 9, 2024

For an array attribute of Active Record model expecting that the query returns an array result.

Try this

$model=Test::find()->select([new \yii\db\Expression('data[1:1]')])->one();

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

No branches or pull requests

2 participants