forrest79 / phpgsql

Simple and fast PHP database library for PostgreSQL with auto converting DB types to PHP and fluent interface for SQL query writing.
Other
11 stars 3 forks source link

Support for PostgreSQL own custom types? #35

Closed C-Duv closed 1 year ago

C-Duv commented 1 year ago

Hello,

It seems the library does not support PostgreSQL custom types.

Say I have the following physical_address type:

CREATE TYPE physical_address AS (
    name text,
    extra_recipient_information text DEFAULT NULL,
    street_number_and_name text,
    extra_street_information text DEFAULT NULL,
    post_office_box text,
    postal_code text,
    locality text,
    country text
);

And a table of clients:

CREATE TABLE IF NOT EXISTS clients (
    id uuid NOT NULL DEFAULT gen_random_uuid(),
    name text NOT NULL,
    address physical_address NOT NULL
);

Containing the following row:

id|name   |address                                         |
--+-------+------------------------------------------------+
… |Foo Bar|(Some Name,42th street,BP123,75001,Paris,France)|

With the following code:

$connection = new Forrest79\PhPgSql\Db\Connection(…);
$some_client = $connection->query(
    'SELECT * FROM "clients" LIMIT 1',
)->fetch();

echo $some_client->address . PHP_EOL;

I get an exception:

Forrest79\PhPgSql\Db\Exceptions\DataTypeParserException: Can't parse type 'physical_address' for value '(Some Name,42th street,BP123,75001,Paris,France)'

Is there a way to make it work?

Can the library access sub fields? Like:

$some_client->address->locality;
$some_client->address['locality'];
forrest79 commented 1 year ago

Hi, you must write your own DataTypeParser for your custom types. It's described here https://github.com/forrest79/PhPgSql/blob/master/docs/db.md#data-type-converting.