A
A
anboo2020-05-07 01:03:10
symfony
anboo, 2020-05-07 01:03:10

Doctrine mark transactions as rollback?

The code:

$this->entityManager->beginTransaction();
$this->entityManager->persist($entityA);
$this->entityManager->flush();
...
$this->entityManager->rollback();

$this->entityManager->persist($entityB);
$this->entityManager->flush();


Transaction commit failed because the transaction has been marked for rollback only


After I call the rollback of the doctrine transaction, it marks the transaction rollbackOnly inside and then it is impossible to call the commit function on em on any of the transactions, consider it as if EM was closed and it can no longer be used if a rollback happened.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abubekov, 2021-01-28
@abubekov

After rollback, you can do this:

if (!$this->entityManager->isOpen()) {
                $entityManager = $this->entityManager->create($this->entityManager->getConnection(), $this->entityManager->getConfiguration(), $this->entityManager->getEventManager());
                $this->serviceManager->setAllowOverride(true);
                $this->serviceManager->setService('entityManager', $entityManager); //Имя сервиса возьмите то которое у вас используется в фабрике или в конфиге доктрины
                $this->serviceManager->setAllowOverride(false);
                $this->entityManager = $this->serviceManager->get('entityManager');
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question