Doctrine Mongo cascade remove children when child is the owning side

1.4k views Asked by At

I have a parent/child OneToMany reference :

/**
 * @ODM\Document
 */
class Parent
{
    // ...

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ODM\ReferenceMany(targetDocument="Child", mappedBy="parent")
     */
    protected $children;

    // ...
}



/**
 * @ODM\Document
 */
class Child
{

    // ...

    /**
     * @var Parent
     * @ODM\ReferenceOne(targetDocument="Parent", inversedBy="children", orphanRemoval=true)
     */
    protected $parent;

    // ...

}

What I want is when a parent gets removed, all its children should be removed as well. I tried cascade={"remove"} and orphanRemoval=true on the parent annotation, but it doesn't seem to work.

I was wondering if there's an option to do this automatically, without having to write a LifeCycleEventListener.

Thanks

1

There are 1 answers

0
BENARD Patrick On BEST ANSWER

And in the parent class ?

class Parent
{
    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ODM\ReferenceMany(targetDocument="Child", mappedBy="parent", cascade={"remove"})
     */
    protected $children;

    // ...
}

In my project, cascade={'remove'} works perfectly, but it's an annotation in the Parent Class, not Child class as I can see in your post.