Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active December 28, 2015 05:09
Show Gist options
  • Save chanmix51/7447181 to your computer and use it in GitHub Desktop.
Save chanmix51/7447181 to your computer and use it in GitHub Desktop.
Using composite types with PgRow
CREATE TYPE postal_address AS (
place text,
postal_code char(5),
city varchar,
cedex char
);
CREATE TABLE customer (
name varchar NOT NULL,
address postal_address NOT NULL CHECK (
(address).place IS NOT NULL
AND
(address).postal_code IS NOT NULL
AND
(address).city IS NOT NULL
)
)
;
INSERT INTO customer (name, address) VALUES (
'john doe',
('27, route des plages', '56590', 'groix', null)
)
;
<?php
namespace My\Database\Type;
use \Pomm\Type\Composite;
class Address extends Composite
{
public $place;
public $postal_code;
public $city;
public $cedex;
}
<?php
// ..
$database->registerConverter(
'PostalAddress',
new \Pomm\Converter\PgRow(
$database,
new \Pomm\Object\RowStructure(
array(
'place' => 'text',
'postal_code' => 'char',
'city' => 'varchar',
'cedex' => 'char'
)),
'\My\Database\Type\AddressType' // <- if not provided, arrays will be returned instead
),
array('public.postal_address', 'postal_address')
)
);
<?php
$customers = $connection
->getMapFor('\MyProject\MySchema\Customer')
->findWhere('name ~ $*', array('^john'));
foreach ($customers as $customer)
{
printf("%s lives in %s (%s).\n",
$customer->name,
$customer->address->city,
$customer->address->postal_code
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment