[DDC-113] Cascaded persist avoids LifecycleCallbacks Created: 04/Nov/09  Updated: 18/Dec/09  Resolved: 18/Dec/09

Status: Closed
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.0-ALPHA3
Fix Version/s: 2.0-ALPHA4
Security Level: All

Type: Bug Priority: Critical
Reporter: Nico Kaiser Assignee: Roman S. Borschel
Resolution: Fixed Votes: 0
Labels: None


When an Entity is created by 'cascade=


', its LifecycleCallbacks (e.g. "PrePersist"!) are not invoked.

When it is persisted explicitly, everything workes fine and the events are called...

Comment by Benjamin Eberlei [ 11/Dec/09 ]

Works for me on trunk, added test-case that proves it (Doctrine\Tests\ORM\Functional\LifecycleCallbackTest::testCascadedEntitiesCallsPrePersist())

Comment by Nico Kaiser [ 14/Dec/09 ]

There is still an issue. You can reproduce it if you change the test case slightly to this:

     * @group DDC-113
    public function testCascadedEntitiesCallsPrePersist()
        $e1 = new LifecycleCallbackTestEntity;
        $e2 = new LifecycleCallbackTestEntity;

        $c = new LifecycleCallbackCascader();
        $c->entities[] = $e1;
        $c->entities[] = $e2;



The difference to the existing (and indeed working) test case is that the Cascader entity is persisted before the collection entries are added.

Comment by Benjamin Eberlei [ 14/Dec/09 ]

That is valid behaviour according to the lifecycle of an entity, persist gets cascaded right and only when ->persist() is called.

In the case you show, the two entities are also not persisted, because the cascade was already executed.

Comment by Nico Kaiser [ 14/Dec/09 ]

From a technical point of view this is ok - but from an intuitive point of view I would think I can use persist whereever I want. So I can create an entity, persist it (so it gets written to the DB), make changes to it (like adding entities to its collection) and getting its saved (and the collection entities persisted) when I call flush().

So I suspect lifecycle events to be called whenever an entity is persisted. And in this case LifecycleCallbackTestEntity's are persisted automatically by flush() (and this is after the Cascader is persisted!), so lifecycle events should fire here.

Comment by Roman S. Borschel [ 17/Dec/09 ]

I have a fix for this ready, however, I want to look into something else before committing. I'll address this over the weekend.

Comment by Roman S. Borschel [ 18/Dec/09 ]

Should be fixed now.

Generated at Wed Nov 25 05:27:44 EST 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.