DDC-872: $em->persist() during an event callback doesn't work, causes subsequent flush() calls to fail #1080

Closed
opened 2026-01-22 13:01:11 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 10, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user felicitus:

Version from GIT

When doing a $em->persist() operation during an event callback, Doctrine2 simply does nothing with the entity. If this should work, it should be fixed. If this shouldn't work, Doctrine should throw an exception.

Example code:


/****
 * @entity
 * @HasLifeCycleCallbacks
 */
class EntityA {
     /****
     * @Id
     * @generatedValue(strategy="AUTO")
     * @Column(type="integer")
     */
    private $id;

    /****
     * @Column
     */
    private $data;


    public function setData ($data) {
        $this->data = $data;
    }

    /****
     * @postPersist
     * @postUpdate
     */
    public function persistSomethingElse()
    {
        $em = \de\netraver\Netraver::getEM(); // Simply returns an instance of the EntityManager

        $entity = new EntityB();
        $entity->setData("testdata");
        $em->persist($entity);

        echo "Persisted EntityB from inside EntityA\n";
    }
}
/****
 * @entity
 */
class EntityB {
     /****
     * @Id
     * @generatedValue(strategy="AUTO")
     * @Column(type="integer")
     */
    private $id;

    /****
     * @Column
     */
    private $data;


    public function setData ($data) {
        $this->data = $data;
    }
}

$test = new EntityA();
$test->setData("Foo");
$em->persist($test);
$em->flush(); // The database should now have 2 objects: one of EntityA and one of EntityB. But EntityB is not persisted.

$entityB = new EntityB();
$entityB->setData("Bar");
$em->persist($entityB);
$em->flush(); // This also doesn't work. This will ONLY work if you remove persist() from the event handler!

As you can see from my comments, Doctrine2 becomes confused if you persist() something inside an event handler. Even a fresh EntityB will not be persisted from the main code unless you remove persist() from EntityA's event handler.

Originally created by @doctrinebot on GitHub (Nov 10, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user felicitus: Version from GIT When doing a $em->persist() operation during an event callback, Doctrine2 simply does **nothing** with the entity. If this should work, it should be fixed. If this shouldn't work, Doctrine should throw an exception. Example code: ``` /**** * @entity * @HasLifeCycleCallbacks */ class EntityA { /**** * @Id * @generatedValue(strategy="AUTO") * @Column(type="integer") */ private $id; /**** * @Column */ private $data; public function setData ($data) { $this->data = $data; } /**** * @postPersist * @postUpdate */ public function persistSomethingElse() { $em = \de\netraver\Netraver::getEM(); // Simply returns an instance of the EntityManager $entity = new EntityB(); $entity->setData("testdata"); $em->persist($entity); echo "Persisted EntityB from inside EntityA\n"; } } ``` ``` /**** * @entity */ class EntityB { /**** * @Id * @generatedValue(strategy="AUTO") * @Column(type="integer") */ private $id; /**** * @Column */ private $data; public function setData ($data) { $this->data = $data; } } ``` ``` $test = new EntityA(); $test->setData("Foo"); $em->persist($test); $em->flush(); // The database should now have 2 objects: one of EntityA and one of EntityB. But EntityB is not persisted. $entityB = new EntityB(); $entityB->setData("Bar"); $em->persist($entityB); $em->flush(); // This also doesn't work. This will ONLY work if you remove persist() from the event handler! ``` As you can see from my comments, Doctrine2 becomes confused if you persist() something inside an event handler. Even a fresh EntityB will not be persisted from the main code unless you remove persist() from EntityA's event handler.
admin added the Bug label 2026-01-22 13:01:11 +01:00
admin closed this issue 2026-01-22 13:01:12 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 10, 2010):

Comment created by @beberlei:

Please see the docs on Events. Calling persist (or any EM method) from lifecycle events is not allowed.

http://www.doctrine-project.org/projects/orm/2.0/docs/reference/events/en#implementing-event-listeners:postupdate,-postremove,-postpersist

If you want to add additional changes during flush you can (mostly) only achieve this using the "onFlush" event:

http://www.doctrine-project.org/projects/orm/2.0/docs/reference/events/en#implementing-event-listeners:onflush

@doctrinebot commented on GitHub (Nov 10, 2010): Comment created by @beberlei: Please see the docs on Events. Calling persist (or any EM method) from lifecycle events is not allowed. http://www.doctrine-project.org/projects/orm/2.0/docs/reference/events/en#implementing-event-listeners:postupdate,-postremove,-postpersist If you want to add additional changes during flush you can (mostly) only achieve this using the "onFlush" event: http://www.doctrine-project.org/projects/orm/2.0/docs/reference/events/en#implementing-event-listeners:onflush
Author
Owner

@doctrinebot commented on GitHub (Nov 10, 2010):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Nov 10, 2010): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1080