A
A
at0m1x2014-04-22 09:06:00
Doctrine ORM
at0m1x, 2014-04-22 09:06:00

What causes The EntityManager is closed error?

I use ORM Doctrine 2 in my project. There is an entity, I'm trying to add a record to the database trace. way:

$entity = new SomeEntity;
$entity->setCreated(new \DateTime())
            ->setSomeField(...);

$this->em->persist($entity);
$this->em->flush($entity);


Throws the The EntityManager is closed exception.

In doctrine/orm/lib/Doctrine/ORM/EntityManager.php there is a closed field, close(), errorIfClosed() and isOpen() methods. I don’t understand what this field is used for and in what case it is set as closed = true;

What does this error mean and what can cause it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BoneFletcher, 2014-04-22
@at0m1x

And what is the result of execution
$this->em->isOpen();before adding a new entity? If false, then an error occurred somewhere before.
Are prePersist, postPersist events handled? The error may be there, especially if flush is called again in them.

F
fenric, 2019-02-17
@fenric

EntityManager closes itself on any ORM-related error, for example, if the database request is incorrect, this is a big problem in projects where PHP process daemonization is used. If your application is based on PSR-15 and PHP-DI, you can keep the EntityManager always open using the following middleware:
https://github.com/autorusltd/doctrine-persistent-...
In any case, even if it's not yours stack, you can at least figure out how to reopen it. But most importantly, control errors in your application, do not complicate it, try to write in such a way that work with the database occurs only in endpoints (repositories that are called in endpoints), and not spread over the project...

S
Sergey, 2014-04-22
Protko @Fesor

So somewhere you close the EntityManager (the close() method is called).
stackoverflow.com/questions/14258591/the-entityman...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question