10

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!

Marc-André
  • 325
  • 2
  • 17
Arne
  • 357
  • 3
  • 8
  • 4
    With a fresh install of S4.2 I confirmed that $this->getDoctrine()->resetManager(); generates the non-lazy error message. I then did "composer require symfony/proxy-manager-bridge" and everything worked. No changes to services.yaml at all. "bin/console debug:container doctrine.orm.default_entity_manager" confirmed the entity manager is lazy. So roll back any changes you might have made to your services file, delete your cache just to be safe, and try again. – Cerad Dec 20 '18 at 13:55
  • 1
    Thank you very much! I've tries it again and you are right. After installing I didn't actually try if it works, since thought I have to set it lazy by myself ("Set the "doctrine.orm.default_entity_manager" service as lazy and require[...]") – Arne Dec 20 '18 at 18:45

1 Answers1

6

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 :(

Arne
  • 357
  • 3
  • 8