0 votes

I am using doctrine 2.0 with the Zend framework. I have the following two entities(snippets) that share bidirectional onetomany relation between them. A user can report multiple bugs.
Bugs Table:

class Bugs
{
    /**
     * @ManyToOne(targetEntity="Users", inversedBy="reportedBugs",cascade={"persist","remove"})
     * @JoinColumn(name="reporter_id", referencedColumnName="id")
     **/
    protected $reporter;
    public function setReporter($reporter)
    {
        $reporter->addReportedBug($this);
        $this->reporter = $reporter;
    }
    }

Users Table

class Users
{
    /**
     * @OneToMany(targetEntity="Bugs", mappedBy="reporter",cascade={"persist","remove"})
     * @var Bugs[]
     **/
    private $reportedBugs = null;
    public function addReportedBug($bug)
    {
        $this->reportedBugs[] = $bug;
    }
}

You can see that cascade = {"remove"} is there in both the sides of relation.

Case1: Now if I remove an entity from the inverse side (Users), all the associated bugs that refer to this user get deleted from the "bugs" table. This is the expected behavior.

Case2: Now if I remove an entity from the owning side(Bugs), the referred User entity is removed from the "users" table, but then it does not cascade this delete to the associated entities of this user entity in the "bugs" table. I mean just like the case 1 all the associated bugs in the "bugs" table should have been removed when a user entity is being removed. I am using simple remove function for this. $em->remove($bug) which should remove the $user referred by $bug and then remove the associated multiple bugs of $user.
Is this an expected outcome?

in Solved by (150 points)
recategorized by

1 Answer

0 votes
Best answer

Hi,

try adding orphanRemoval=true to the Users entity:

/**
  * @OneToMany(targetEntity="Bugs", mappedBy="reporter",cascade={"persist","remove"}, orphanRemoval=true)
  * @var Bugs[]
  **/
by (550 points)
selected by