Doctrine2 how to deal with updating entities after rollback? ("The EntityManager is closed") Doctrine2 how to deal with updating entities after rollback? ("The EntityManager is closed") php php

Doctrine2 how to deal with updating entities after rollback? ("The EntityManager is closed")


Short answer

Use two entitymanagers. One for the potentially unsafe operations and one for logging/reporting on the other.

Long answer

Generally, you cannot make sure that errors don't happen (some errors don't happen before you flush to the database). And once they happen, the entitymanager is closed for business.

Here's what I do (excerpts from config.yml):

doctrine:    orm:        default_entity_manager: 'default'        entity_managers:            default:                mappings: { ... }            logging:                mappings: { ... }

For normal operations, I use the default entity manager, which requires no change to your code.

For meta-operations (like logging the progress or result of a batched import or something similar), I explicitly fetch the 'logging' manager and use it for creating/updating the logging/report entities (and only for those).


In this particular example you are adding specification to Report. So can you this this?

$entityManager->clear("Your\Bundle\Entity\Specification");

and then do as you proposed:

// Store error info:$report->setState('error');$entityManager->persist($report);$entityManager->flush(); // all hell breaks loose in here

Also, I think doing persist on object with assigned ID is invalid. ( $report object in catch branch)