Aliasing fields using partial object syntax + array hydrator in doctrine 2

Tags: , , ,



Is there any way to alias fields when using partial object syntax in Doctrine 2?

I know I can do this:

$this->createQueryBuilder('user')->select([
     'user.id AS id',
     'user.firstName AS first_name',
     'user.lastName AS last_name',
     'user.email AS email',
     'user.dateCreated AS date_created'
])->getQuery()->getArrayResult();

However I need to use the partial object syntax in order for doctrine to retrieve the result in a nested relational heirarchy:

$this->createQueryBuilder('team')
    ->select('PARTIAL team.{id, name, dateCreated}, s, PARTIAL e.{id, name}')
    ->innerJoin('team.session', 's')
    ->innerJoin('s.event', 'e')
    ->getQuery()->getArrayResult();

I dug around in DoctrineORMInternalHydrationArrayHydrator but didn’t see any hooks or anything, and it doesn’t look like Doctrine has a postSelect event or something that would allow me to implement my own mutation.

Thanks for any help!

Answer

Not very efficient, but I ended up subclassing the ArrayHydrator and mutating the keys myself.

Hopefully there is a better way, if not I hope this helps someone



Source: stackoverflow