Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about Query::HINT_INCLUDE_META_COLUMNS for custom hydrators #11461

Open
wants to merge 1 commit into
base: 2.19.x
Choose a base branch
from

Conversation

Korbeil
Copy link

@Korbeil Korbeil commented May 20, 2024

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 😉

@Korbeil Korbeil changed the base branch from 3.1.x to 2.19.x May 20, 2024 15:00
@Korbeil Korbeil changed the base branch from 2.19.x to 3.1.x May 20, 2024 15:00
@Korbeil Korbeil changed the base branch from 3.1.x to 2.19.x May 20, 2024 15:02
greg0ire
greg0ire previously approved these changes May 20, 2024
@Korbeil
Copy link
Author

Korbeil commented May 20, 2024

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 Korbeil marked this pull request as draft May 20, 2024 15:23
@Korbeil
Copy link
Author

Korbeil commented May 20, 2024

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 Korbeil marked this pull request as ready for review May 20, 2024 15:58
@greg0ire greg0ire dismissed their stale review May 20, 2024 16:07

I need to think this through

@@ -1370,6 +1370,9 @@ userland:
- ``Query::HINT_CUSTOM_TREE_WALKERS`` - An array of additional
``Doctrine\ORM\Query\TreeWalker`` instances that are attached to
the DQL query parsing process.
- ``Query::HINT_INCLUDE_META_COLUMNS`` - A boolean that causes
meta columns like foreign keys and discriminator columns to be
selected and returned as part of the query result.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not really usable, then we should maybe not document it at all, or at least let the users who are curious about it know that it's used internally. Like you, they might be able to leverage it through a few hacks, but it's clearly not designed to be used outside Doctrine itself. That could be challenged, and the design improved, I suppose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants