orm icon indicating copy to clipboard operation
orm copied to clipboard

Add note about `Query::HINT_INCLUDE_META_COLUMNS` for custom hydrators

Open Korbeil opened this issue 1 year ago • 2 comments

Hey !

I was trying to create a custom hydrator that handle everything the same as ObjectHydrator and I was not understanding WHY I don't have meta columns when using my hydrator while ObjectHydrator had them. After some search I found: https://github.com/doctrine/orm/blob/3.1.x/src/Query/SqlWalker.php#L647-L648 that forces meta columns when using ObjectHydrator but for any other hydrator you have to pass the hint.

So I added a note about that hint in the custom hydrator documentation so people knows about this :wink:

Korbeil avatar May 20 '24 14:05 Korbeil

Actually using this doesn't work because Query and SqlWalker are executed before the custom hydrator is called so the hint I set in my prepare method is not used at all:

1^ "sqlWalker"
2^ []
^ array:1 [
  0 => "c0_.id AS id_0, c0_.name AS name_1"
]
1^ "customHydrator.prepare"
2^ array:2 [
  "deferEagerLoad" => true
  "doctrine.includeMetaColumns" => true
]

Korbeil avatar May 20 '24 15:05 Korbeil

After a lot of thinking, I found a (not good looking, but working) method to make this works, I still think it should be documentated somewhere that this hint exists and what it does. Also, I do think we should allow hydrators to tells when they need meta columns but as the current Doctrine architecture. How and when hydrator are called make this almost impossible.

For my use-case I made a static method:

public static function getResult(Query $query): mixed
{
    $query->setHint(Query::HINT_INCLUDE_META_COLUMNS, true);
    return $query->getResult('automapper');
}

And then, when you want the result of a query:

$qb = $userRepository->createQueryBuilder('u');
$resultAutoMapper = AutoMapperHydrator::getResult($qb->getQuery());

It's not that bad and doesn't require too much for the end-user but I still think it would be better if we could set this elsewhere.

Korbeil avatar May 20 '24 15:05 Korbeil

There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days. If you want to continue working on it, please leave a comment.

github-actions[bot] avatar Oct 15 '24 03:10 github-actions[bot]

This pull request was closed due to inactivity.

github-actions[bot] avatar Oct 23 '24 03:10 github-actions[bot]