QueryBuilder: Query hints not remembered on getQuery() (Returns a fresh object every time). Add hint option to QueryBuilder. #7479

Open
opened 2026-01-22 15:52:10 +01:00 by admin · 0 comments
Owner

Originally created by @Jalliuz on GitHub (Feb 27, 2025).

Feature Request

What

It would be nice if we could set query hints on the QueryBuilder so they are added to the new Query object when you do ->getQuery()

Why

I have some utility classes for optimising the query, for example:

  • function addDistinctWhenToMany(QueryBuilder $qb): Check if we have a toMany join, if so add "$qb->distinct" and $qb->getQuery()->setHint(Query\SqlWalker::HINT_DISTINCT, true)

The reason for doing this is because blindly adding "distinct" to the query lowers performance so I only want to add it if its needed.
But if later in the process you do getQuery() again, all the previously set hints are gone because it creates a "new Query()" every time

The above is just an example, I have other use cases to set an output_walker hint if necessary and so on.

How

Adding an array to the QueryBuilder "customHints" or something like that.
And every time ->getQuery gets executed, the customHints from QueryBuilder are taken into account and added automatically

Originally created by @Jalliuz on GitHub (Feb 27, 2025). ### Feature Request #### What It would be nice if we could set query hints on the QueryBuilder so they are added to the new Query object when you do ->getQuery() #### Why I have some utility classes for optimising the query, for example: * function addDistinctWhenToMany(QueryBuilder $qb): Check if we have a toMany join, if so add "$qb->distinct" and $qb->getQuery()->setHint(Query\SqlWalker::HINT_DISTINCT, true) The reason for doing this is because blindly adding "distinct" to the query lowers performance so I only want to add it if its needed. But if later in the process you do getQuery() again, all the previously set hints are gone because it creates a "new Query()" every time The above is just an example, I have other use cases to set an output_walker hint if necessary and so on. #### How Adding an array to the QueryBuilder "customHints" or something like that. And every time ->getQuery gets executed, the customHints from QueryBuilder are taken into account and added automatically
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7479