DDC-172: Cascading persists doesn't seem to work with OneToMany relationship #212

Closed
opened 2026-01-22 12:30:50 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 23, 2009).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user ablock:

Given the following entities:

namespace Entities;

class Parent {
    /****
     * @OneToMany(targetEntity="Child", mappedBy="parent", cascade={"persist"})
     */
    private $children;
}

class Child {
    /****
     * @ManyToOne(targetEntity="Parent", cascade={"persist"})
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;
}

And the following code:

$p = new Parent;
$em->persist($org);
$em->flush();

$c = new Child;
// This method maintains the bidirectional relationship on the application side
$p->addChild($c);

$em->clear();
$id = $p->getId();

$org = $em->find('Entities\Parent', $id);
echo count($c->getChildren());

Should output "1" but instead outputs "0"

Originally created by @doctrinebot on GitHub (Nov 23, 2009). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user ablock: Given the following entities: ``` namespace Entities; class Parent { /**** * @OneToMany(targetEntity="Child", mappedBy="parent", cascade={"persist"}) */ private $children; } class Child { /**** * @ManyToOne(targetEntity="Parent", cascade={"persist"}) * @JoinColumn(name="parent_id", referencedColumnName="id") */ private $parent; } ``` And the following code: ``` $p = new Parent; $em->persist($org); $em->flush(); $c = new Child; // This method maintains the bidirectional relationship on the application side $p->addChild($c); $em->clear(); $id = $p->getId(); $org = $em->find('Entities\Parent', $id); echo count($c->getChildren()); ``` Should output "1" but instead outputs "0"
admin added the Bug label 2026-01-22 12:30:51 +01:00
admin closed this issue 2026-01-22 12:30:51 +01:00
Author
Owner

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

Comment created by @beberlei:

cascade-persists are executed when persist() is called, in your case before you add the children. The test-case shows how the behaviour works:

    public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
        $this->product->addFeature($this->firstFeature);
        $this->product->addFeature($this->secondFeature);
        $this->_em->persist($this->product);
        $this->_em->flush();

        $this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature);
        $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
    }

You have to add the children before calling persist.

@doctrinebot commented on GitHub (Feb 10, 2010): Comment created by @beberlei: cascade-persists are executed when persist() is called, in your case before you add the children. The test-case shows how the behaviour works: ``` public function testSavesAOneToManyAssociationWithCascadeSaveSet() { $this->product->addFeature($this->firstFeature); $this->product->addFeature($this->secondFeature); $this->_em->persist($this->product); $this->_em->flush(); $this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature); $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature); } ``` You have to add the children before calling persist.
Author
Owner

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

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 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#212