5

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?

Florent
  • 12,310
  • 10
  • 49
  • 58
Thomas Lomas
  • 1,553
  • 10
  • 22

1 Answers1

3

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.

moonwave99
  • 21,957
  • 3
  • 43
  • 64