Skip to content

Commit

Permalink
Work with array columns (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdef authored Feb 1, 2023
1 parent 6f919ac commit 626ac91
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/CommandPDOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,25 @@ public function testBindParamsNonWhere(string $sql): void
{
parent::testBindParamsNonWhere($sql);
}

/**
* {@link https://github.com/yiisoft/db-pgsql/issues/1}
*/
public function testInsertAndReadToArrayColumn(): void
{
$db = $this->getConnection(true);

$arrValue = [1, 2, 3, 4];
$insertedData = $db->createCommand()->insertWithReturningPks('{{%table_with_array_col}}', ['array_col' => $arrValue]);

$this->assertGreaterThan(0, $insertedData['id']);

$selectData = $db->createCommand('select * from {{%table_with_array_col}} where id=:id', $insertedData)->queryOne();

$this->assertEquals('{1,2,3,4}', $selectData['array_col']);

$columnSchema = $db->getTableSchema('{{%table_with_array_col}}')->getColumn('array_col');

$this->assertSame($arrValue, $columnSchema->phpTypecast($selectData['array_col']));
}
}
6 changes: 6 additions & 0 deletions tests/Support/Fixture/pgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ DROP TABLE IF EXISTS "T_constraints_2";
DROP TABLE IF EXISTS "T_constraints_1";
DROP TABLE IF EXISTS "T_upsert";
DROP TABLE IF EXISTS "T_upsert_1";
DROP TABLE IF EXISTS "table_with_array_col";

DROP SCHEMA IF EXISTS "schema1" CASCADE;
DROP SCHEMA IF EXISTS "schema2" CASCADE;
Expand Down Expand Up @@ -425,3 +426,8 @@ CREATE TABLE "schema2"."custom_type_test_table" (
);
INSERT INTO "schema2"."custom_type_test_table" ("test_type", "test_type2")
VALUES (array['VAL1']::"my_type"[], array['VAL2']::"schema2"."my_type2"[]);

CREATE TABLE "table_with_array_col" (
"id" SERIAL NOT NULL PRIMARY KEY,
"array_col" integer ARRAY[4]
);

0 comments on commit 626ac91

Please sign in to comment.