8000 Weird behaviour due to EM resets after update to 2.0.6 · Issue #1114 · doctrine/DoctrineBundle · GitHub
[go: up one dir, main page]

Skip to content
Weird behaviour due to EM resets after update to 2.0.6 #1114
@acasademont

Description

@acasademont

Hi,

PHP-PM used to manually reset or clear the entity manager after every request (https://github.com/php-pm/php-pm-httpkernel/blob/master/Bootstraps/Symfony.php#L168). Now it seems like Symfony is able to automatically do it thanks to the kernel.reset tag but we're seeing a very strange behaviour.

On the first request all is fine,
On the second request, the EntityManager is reset at the beginning of the request, but the first ORM query is still going to the old EntityManager. The second ORM query sees that the manager has been reset, and instantiates another EM/UnitOfWork, effectively clearing the entityStates cache, which is bad, because in my code I have an entity created by the first query that the newly created UnitOfWork doesn't know anything about. If it is persisted, it will try an INSERT, instead of an UPDATE :(

To be honest, I don't really understand why the first ORM query after the reset is still using the old EM/UnitOfWork.

In any case, the EM shouldn't be reset after every request, a simple clear should be enough unless the EM hasn't been closed due to an Exception. In that case, yes, the manager should be reset. We've been working with that code in PHP-PM for a very long time and it works pretty well.

            foreach ($doctrineRegistry->getManagers() as $curManagerName => $curManager) {
                if (!$curManager->isOpen()) {
                    $doctrineRegistry->resetManager($curManagerName);
                } else {
                    $curManager->clear();
                }
            }

We've reverted to 2.0.2 in the meantime, but I'm not entirely sure how to solve this issue going forward.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0