Doctrine preLoad Listener to Manipulate QueryBuild

2019-08-17 04:03发布

问题:

Is there a way to manipulate a Doctrine 2.6 QueryBuilder (DQL) before the query is sent to the database?

So for example, if we always want two entities to be associated (without lazy loading) then we need to JOIN the other entity's table (for efficiency). So it would be great if we could check if the other entity was joined yet, and if not, join it automatically.

But doctrine doesn't have a preLoad listener...

回答1:

You can achieve your goal another way. You can set eager mode for fetching entities in annotations:

/**
 * @ORM\ManyToOne(..., fetch="EAGER")
 */

For what else do you need preLoad listener?

If you want just modify every generated SQL before it will be executed you can use SQL Filters.