mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Loading meta columns in custom hydrator #5499
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @iluuu1994 on GitHub (Apr 12, 2017).
I'm writing a custom hydrator for Gedmo Tree.
The hydrator accesses the primary key of the
parentproperty of a tree node:Unfortunately, the primary key is actually
nullif the query doesn't contain theHINT_INCLUDE_META_COLUMNShint. I tracked it down to these lines in the Doctrine repository. Apparently, only the object hydrator, but not its subclasses, get the meta columns.So I was able to make it work by replacing those lines with the following:
This will include the meta columns for the object hydrator or any of its subclasses.
Obviously the
ReflectionClassadds some overhead. I'm not sure how relevant performance is to this particular method. Is this reasonable? Is there a better way to do this? Should I create a pull request?Thanks for taking the time to read and respond!
@iluuu1994 commented on GitHub (Jun 1, 2017):
@Ocramius @guilhermeblanco Sorry to bother again.
Is the suggestion above appropriate? If it isn't I'll close the issue.
@Ocramius commented on GitHub (Jun 6, 2017):
@iluuu1994 you did a good analysis of what is missing in the implementation, but I still have no idea what this is for. Consider making a practical example in a test case?
@iluuu1994 commented on GitHub (Jun 6, 2017):
@Ocramius Basically, the hydrator needs a way to access the ID of a
ManyToOnerelationship (parentin a Gedmo tree). Since most of the time you wouldn't want to map the foreign key directly the hydrator gets the ID from theparentproxy object.Unfortunately, the proxy object is only created if you use the object hydrator or set the
HINT_INCLUDE_META_COLUMNShint. But it is not created when you inherit from the object hydrator. Thus, the solution above checks if$hydratorClass->isSubclassOf('Doctrine\ORM\Internal\Hydration\ObjectHydrator').Yes, I'll do that.
@Ocramius commented on GitHub (Jun 6, 2017):
Specifically this bit:
This is a bit unclear.
I'll have to wait for a test to see what's going on, sorry.
@iluuu1994 commented on GitHub (Jun 6, 2017):
@Ocramius Consider a simple tree entity:
The goal of the hydrator is to create a tree from the flat entity array returned from the query. To do this, the hydrator creates a big parent-child hashmap:
This is why I need to access the parent ID of the current node.
That just means that I don't want to add a new
parent_idproperty to the Node but instead access the proxy object which is only possible if the$addMetaColumnscolumns is set totrue.@guilhermeblanco commented on GitHub (Jun 6, 2017):
@iluuu1994 I understand the problem. Your solution is valid (I'd just make the comparison as
===in the first line).However, to properly address this issue (and make it future proof), could you try to make a PR and also include a test case supporting this fix?
@iluuu1994 commented on GitHub (Jun 6, 2017):
@guilhermeblanco Great! I used the
==operator to match the current implementation and thus not change the behaviour. I'll create a patch including tests.Thank you @Ocramius and @guilhermeblanco for reviewing!
@vctls commented on GitHub (Jan 31, 2019):
Hi! Any news on this subject?
I had the same issue with a custom hydrator, and the suggested fix did the trick.
I'm not sure if requiring a subclass of the ObjectHydrator is the best way to go, especially since the ObjectHydrator looks a bit scary and not very extendable, but hey, it works.
@iluuu1994 commented on GitHub (Jan 31, 2019):
I never got around to it. Feel free to take over. The fix is really simple, all you need is the code from the comment above. Creating a test should also be rather simple.
@anboo commented on GitHub (Mar 12, 2021):
You can provide hint:
And all you relations will be filled automatically by RSM parser.
@Tofandel commented on GitHub (May 16, 2022):
I was bashing my head wondering why it wasn't working with symfony's query builder when using
$qb->getQuery()->setHint(Query::HINT_INCLUDE_META_COLUMNS, true)It turns out that every call to ->getQuery() creates a new query and so the hints and hydration mode are lost if you write
So you need to keep the query and never use getQuery multiple times
It's a bit of a design issue on symfony's side, but it might save headaches to some other people