Clear the doctrine entity manager in every request #127
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have been trying a lot to find an alternative to this, but unfortunately not clearing Doctrine at the end of the request causes 2 major issues:
Once an entity is fetched from the database, Doctrine will not fetch it again in the next request, but reuse the same entity. This is good for performance, but makes it extremely easy to have inconsistent state, because any changes made to an entity or a child collection will get carried over to the next request, regardless if the entity was persisted or not. This causes bugs like PHP-PM issue with taxons setParent Sylius/Sylius#9629 that are both very hard to track down and to fix.
Because entities are cached and not fetched again in the next request, any changes made to the database from an external source (e.g. a worker or third party script) are not detected by the app, which continues to use the cached version of the entity as long as the worker remains alive.
This PR fixes this by clearing the entity manager at the end of each request, so that fresh queries will be performed in the next.