Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/database-pgsql/src/Connection/PgSqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ private function bindValues(
): void {
foreach ($bindings as $key => $value) {
$param = is_int($key) ? $key + 1 : $key;

if (is_array($value)) {
$statement->bindValue($param, json_encode($value), PDO::PARAM_STR);
continue;
}

$type = match (true) {
is_bool($value) => PDO::PARAM_BOOL,
is_null($value) => PDO::PARAM_NULL,
Expand Down
29 changes: 29 additions & 0 deletions packages/database-pgsql/tests/Connection/PgSqlConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,4 +685,33 @@ protected function createPdo(
expect(fn () => $connection->beginTransaction())
->toThrow(TransactionException::class, 'Nested transactions are not supported');
});

it('JSON-encodes array bindings instead of casting them to the string "Array"', function (): void {
$config = createTestPgSqlConfig();
$connection = new class ($config) extends PgSqlConnection
{
protected function createPdo(
string $dsn,
string $username,
string $password,
array $options,
): PDO {
$pdo = createSqliteMockPdo($options);
$pdo->exec('CREATE TABLE items (id INTEGER PRIMARY KEY, metadata TEXT)');

return $pdo;
}
};

$connection->execute(
'INSERT INTO items (metadata) VALUES (?)',
[['key' => 'value', 'nested' => [1, 2, 3]]],
);

$rows = $connection->query('SELECT metadata FROM items');

expect($rows[0]['metadata'])
->toBe('{"key":"value","nested":[1,2,3]}')
->not->toBe('Array');
});
});