Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark a Doctrine Entity Manager as "lazy" in Symfony 4.2

After looking for a solution on how to reset a Doctrine Entity Manager after a duplicate key exception, I found this thread: The entitity manager is closed

One of the answers mentions the resetManager() method, which seems to be correct according to the documentation: $em = $this->getDoctrine()->resetManager();

Unfortunaly, when calling resetManager(), I get the following excpetion:

Resetting a non-lazy manager service is not supported. Set the "doctrine.orm.default_entity_manager" service as lazy and require "symfony/proxy-manager-bridge" in your composer.json file instead.

I've installed the package but can't find a way to mark the default manager as lazy. I have tried in my services.yaml:

doctrine.orm.default_entity_manager:
    lazy: true

But this lead to further errors. How do I define the default entity manager as lazy without having to re-specify the manager completely? The documentation for lazy services also didn't really help.

Thanks!

like image 716
Arne Avatar asked Dec 19 '18 18:12

Arne


1 Answers

As Cerad wrote in the comment, it is not required to do anything else beside of installing "symfony/proxy-manager-bridge". The rest will happen with Symfony magic.

Be aware that you can not use the existing entity repositories anymore with the new manager. So if you got your repositories injected / autowired before, you have to re-assign them by yourself with new ones from the new manager.

The same goes for your entity objects. If for example you still have a $parent entity object, create a new $child entity and assign the $child to the $parent in a relation, persisting and flushing will fail since the new manager finds detached objects.

I have tried to get it working with the existing objects via the merge method of the manager, but ended up with other errors so I am just querying them again via the new manager now :(

like image 167
Arne Avatar answered Nov 05 '22 12:11

Arne