I have a problem that removeElement function doesn't delete from database the many to many element. For understanding better, I will present my code:
class User {
/**
* @var Collection|CandidateSocialLink[]
*
* @ORM\OneToMany(targetEntity="UserSocialLink", mappedBy="user", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="user_social_link_id", referencedColumnName="id")
*
*/
private $socialLinks;
class UserSocialLink {
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="socialLinks", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* */
private $user;
/**
* @ORM\ManyToOne(targetEntity="SocialLink", cascade={"persist", "remove"}))
* @ORM\JoinColumn(name="social_link_id", referencedColumnName="id")
*
* */
private $socialLink;
}
class SocialLink {
- frankly I don't have anything related to this relationship because I didn't want to have many things that I don't use.
}
I have a function in User:
public function removeSocialLink(UserSocialLink $removeSocialLink)
{
if ($this->socialLinks->contains($removeSocialLink)) {
$this->socialLinks->removeElement($removeSocialLink);
}
return $this;
}
And then I call this function:
public function save(CandidateInterface $candidate, array $arguments = ['flush' => true]): CandidateInterface
{
$this->getEntityManager()->persist($candidate);
if (true === $arguments['flush']) {
$this->getEntityManager()->flush();
}
return $candidate;
}
When I return the entity after this operation, the entity doesn't have the social links, but in the db still appears. I think I'm not doing something good regarding the annotation, but what? If any of you has some opinion, please share.