yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Incorrect parsing PostgreSQL response for "ARRAY" columns #20250

Open abolotin opened 4 weeks ago

abolotin commented 4 weeks ago

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)