ManyToMany does not delete

I am trying to remove ManyToMany relationships inside Doctrine 2. I have two entities - Userand TargetGroup.

In my object User, I have:

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

In my object TargetGroup, I have:

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

I'm trying to call:

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

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

Two methods used:

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

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

This is not an error, but it also does not fulfill any removal requests.

Any suggestions?

+5
source share
1 answer

See the Doctrine cascade property , disconnect in your case.

If you set cascade={"detach"}in both annotations ManyToMany, cross-stable entries should stop persisting.

+3
source

All Articles