-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Getting an Entity from an injected Repository in a Service returns a detached Entity from versions >= 1.6.4 #841
Comments
I'm having the exact same problem (Doctrine Bundle 1.9.1, ORM 2.6), This bug also occurs if an entity doesn't have an attached entity listener and has its repository injected into an entity listener either directly or as a sub dependency.. |
You're dealing with circular dependency issue here.
Solution - declare your listeners and repositories as lazy. |
@supersmile2009 is correct: entity listeners should be declared as lazy services. With 1.11, we'll always lazy load listeners, so you don't have to update your configuration anymore (see #899). |
@supersmile2009 @alcaeus I just tried to turn both the listener and the repository as lazy in the debug project I made for this issue : unexpected-doctrine-entity-state-in-service and it still not work. Did I missed something ? |
That's not the reply I was hoping for. I'll take a look at the project and try to reproduce it in a test case. Thanks for following up! |
Thank you too ! Just a little note about the project : in the services.yml, SuperListener & SuperService are loaded by the AppBundle\ default section. You have to alias them or comment this section/exclude them to be sure they are loaded via their own declaration (present below) and then be able to lazy load. (I had let this file as is to be able to test many configurations) |
So, after taking another look I figured out what's going on. First of all, please don't take the first point personally: you are using a 2.5 year old version of the bundle; at the time you created the issue it was already 2 years old. The first thing you should always do is try to reproduce it in a current version, as that makes my job significantly easier. Either way, I took the time to debug this to figure out what's going on. The first load cycle is triggered by the ParamConverter: since the controller is being called, the DoctrineParamConverter is asked if it supports the So now, your repository has a different entity manager instance than the one stored in the container, which is why your entities are going to be detached after fetching them using the repository. There are a few ways around this:
TL;DR: please update to a supported version of DoctrineBundle (or at least one with support for lazy entity listeners) and make the listener lazy. |
@alcaeus Ok I understand. I effectively used an old version of the Bundle (1.6.4) in this debug project, because I've chosen the first version in which I detected that it wasn't working as expected. I had no idea about the fact that tagging the entity listener as lazy would work only from 1.6.5, sorry for that. Thanks again for time and your detailed explanations ! |
Getting an Entity from an injected Repository in a Service returns a detached Entity for versions of DoctrineBundle >= 1.6.4, due to the following merge : Set doctrine.orm.entity_manager.abstract as lazy #559
This bug occurs with the following conditions:
Version of doctrine/doctrine-bundle >= 1.6.4
the Entity has an attached EntityListener
The Service is injected into the EntityListener
I'm not sure if it should be considered as a bug or not, maybe this is a wanted behavior but I don't think so.
I made a small project in order to test it easily : unexpected-doctrine-entity-state-in-service
I've tested with Symfony 3.4, not with other versions yet.
Note : Before finding the origin of the bug, I posted on StackOverflow, in case of you are interested : https://stackoverflow.com/questions/51426933/symfony-3-4-unexpected-doctrine-entity-state-in-service
The text was updated successfully, but these errors were encountered: