John Smith
John Smith

Reputation: 6197

Flush in entity causes infinite loop (Doctrine 2)

there are two entities:

A: which will auto update something on B
B

in A there is a preupdate:

@ORM\PreUpdate
public function asd()
{
    foreach (\Doctrine\Repository\B::getInstance()->findxxxx($sdf) as $x)
    {
        $em->remove($x);
        $em->flush(); *******
    }
}

I traced the SQL log:

Doctrine\Entity\Accommodation.php: 1116
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Event\ListenersInvoker.php: 102
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php: 1064
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php: 384
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php: 356
Doctrine\Entity\Accommodation.php: 1116
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Event\ListenersInvoker.php: 102
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php: 1064
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php: 384
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php: 356
Doctrine\Entity\Accommodation.php: 1116
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Event\ListenersInvoker.php: 102
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php: 1064
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php: 384
Doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php: 356
Doctrine\Entity\Accommodation.php: 1116 

so it turned out the line with ******* causes this infinitive loop. Im inside a transaction. I know its not much, but can someody help me?

Upvotes: 1

Views: 1171

Answers (1)

Richard
Richard

Reputation: 4119

You shouldn't be using remove-> inside a PreUpdate and that is the likely cause of your problem.

See the documentation on PreUpdate, note the section down the bottom "restrictions for this event".

You should look at using a different event like onFlush or prePersist. In your case I would write that as an event listener, not as an entity method as you'll then have access to the unit of work and other useful stuff.

Upvotes: 1

Related Questions