In a ZF2 - PostgreSQL application, I want to use Doctrine2's own queries to create paginator lists.
Thus, it works great if any user-defined type Doctrine / Pgsql is selected. But for a single request, I would use data from a user type.
I have a custom Doctrine 2 type called AlertRecipientdeclared in PostgreSQL, like below:
CREATE TYPE alert_recipient AS (
email text,
status int
);
This type is used in some tables. example in the table clients:
ID (int) | name (varchar) | alerts (alert_recipients[])
1 | John Doe | {"(john@doe.com, 1), (jane@doe.com, 1)"}
2 | Foo Bar | {"(foo@bar.com, 1)"}
( alert_recipient[]extends alert_recipientto save a list of many entries alert_recipient)
This type is associated with an entity for hydration:
class AlertRecipient
{
protected $email;
protected $status;
}
Doctrine registration is done in the event onBoostrap:
if (!Type::hasType('alert_recipient'))
{
Type::addType('alert_recipient', AlertRecipient::class);
}
$platform->registerDoctrineTypeMapping('alert_recipient', 'alert_recipient');
if (!Type::hasType('alert_recipient[]'))
{
Type::addType('alert_recipient[]', AlertRecipients::class);
}
$platform->registerDoctrineTypeMapping('_alert_recipient', 'alert_recipient[]');
, doc http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html
:
$rsm = new ResultSetMappingBuilder($em);
$rsm->addRootEntityFromClassMetadata(Client::class, 'c');
$query = 'SELECT c.* FROM clients c JOIN ...';
$em->createNativeQuery($query, $rsm);
$results = $query->getResult(NativeQuery::HYDRATE_ARRAY);
, find() Doctrine native, .
:
public function convertToPHPValue($value, AbstractPlatform $platform)
{
var_dump($value); exit;
}
Edit
public function convertToPHPValueSQL($sqlExpr, $platform)
{
return 'to_json(' . $sqlExpr . ')';
}
find() AlertRecipient: string '[{"email":"john@doe.com","status":1}, {"email":"jane@doe.com","status":1}]'
['order_emails'] AlertRecipient: string '{"(john@doe.com, 1), (jane@doe.com, 1)"}'
...
Edit
AlertRecipient Client:
protected $alertRecipients = [];