Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top