Doctrine2 Native Request Does Not Humidify Custom Type

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;

    // ... with accessors
}

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');
// ... some other data from join entity (e.g)

$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:

/* @ORM\Column(type="alert_recipient[]", nullable=true, name="alert_recipients")
 * @Gedmo\Versioned
 */
 protected $alertRecipients = [];

 // ... with accessors 
+4
1

Doctrine 2 ?

, convertToPHPValue convertToDatabaseValue .

:

find() , AlertRecipient...

, AlertRecipient dbal Type, Entity.

( AlertRecipient) Doctrine\DBAL\Types\Type.

, , :

/** @Column(type="alert_recipient") */
$alertRecipient;

( , , ), , , , , , .

0

Source: https://habr.com/ru/post/1668866/


All Articles