Thomas Lomas
Thomas Lomas

Reputation: 1553

ManyToMany is not deleting

I am trying to remove a ManyToMany relationship inside Doctrine 2. I have two entities - User and TargetGroup.

In my User entity I have:

/**
 * @ORM\ManyToMany(targetEntity="TargetGroup", inversedBy="users")
 */
private $targetGroups;

In my TargetGroup entity I have:

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="targetGroups")
 */
private $users;

I am trying to call:

$user->removeTargetGroup($targetGroup);
$targetGroup->removeUser($user);

$em->persist($user);
$em->persist($targetGroup);
$em->flush();

The two methods used are:

public function removeTargetGroup(Path To Bundle $targetGroups)
{
    $this->targetGroups->removeElement($targetGroups);
}

public function removeUser(Path To Bundle $users)
{
    $this->users->removeElement($users);
}

It does not error, but it does not do any of the delete queries either.

Any suggestions?

Upvotes: 5

Views: 236

Answers (1)

moonwave99
moonwave99

Reputation: 22817

Have a look at Doctrine cascade property, detach in your case.

If you set cascade={"detach"} on both ManyToMany annotations, crosstable records should drop on persist.

Upvotes: 3

Related Questions