You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All row data (columns) are automatically parsed to the correct PHP types (detected from the DB column type - but no DB structure is read, PG send type of all columns in the result).
316
316
317
-
You can get your own data (manually passed, not from DB) parsed to the same type as have the column in the result:
318
-
319
-
```php
320
-
$result = $connection->query('SELECT id, nick, active FROM users');
321
-
322
-
$data = $result->parseColumnValue('id', '123');
323
-
dump($data); // (integer) 123
324
-
```
325
-
326
317
On the result object, we can also check what columns were accessed in our application. You can check this before your request ends, and you can get possible columns that are unnecessary to be selected from the DB:
327
318
328
319
```php
@@ -655,8 +646,6 @@ table($rows);
655
646
656
647
> There are some PostgreSQL types that are hard to convert to the PHP type (some types of arrays, hstore...), and these types can be simply converted to the JSON in a DB, and this JSON can be simply converted in PHP. The parser throws an exception and gives you a hint - convert type in SELECT to JSON. If you need parsing without converting to JSON, you need to write your own PHP logic (and you can create a pull-request for this :-)).
657
648
658
-
> Internal info: There is the interface `Forrest79\PhPgSql\Db\ColumnValueParser` that is required by the `Row` object. Main implementation is in the `Result` object, that makes the real values parsing. Second is the `DummyColumnValueParser` that is used for manually created rows or for unserialized rows and this parser does nothing and just return a value.
659
-
660
649
### How to extend default data type parsing
661
650
662
651
If you need to parse some special DB type, you have two options. You can create your own data type parser implementing interface `Forrest79\PhPgSql\Db\DataTypeParser` with the only one public function `parse(string $type, string|null $value): mixed`, that get DB type and value as `string` (or `null`) and return PHP value. The second option is preferable - you can extend existing `Forrest79\PhPgSql\Db\DataTypeParsers\Basic` and only add new/update existing types.
0 commit comments