多对多实体无法保存在db Symfony中

时间:2018-08-06 20:52:45

标签: symfony many-to-many

我有一个问题,即removeElement函数不会从数据库中删除多对多元素。为了更好地理解,我将展示我的代码:

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.
}

我在User中有一个功能:

public function removeSocialLink(UserSocialLink $removeSocialLink)
{
    if ($this->socialLinks->contains($removeSocialLink)) {
        $this->socialLinks->removeElement($removeSocialLink);
    }

    return $this;
}

然后我调用此函数:

public function save(CandidateInterface $candidate, array $arguments = ['flush' => true]): CandidateInterface
{
    $this->getEntityManager()->persist($candidate);

    if (true === $arguments['flush']) {
        $this->getEntityManager()->flush();
    }

    return $candidate;
}

当我在执行此操作后返回实体时,该实体没有社交链接,但是在数据库中仍然出现。 我认为我在注释方面做得不好,但是呢? 如果您有任何意见,请分享。

2 个答案:

答案 0 :(得分:0)

在您的情况下,教义不考虑您对socialLinks数组所做的更改来强制更改克隆该数组:

public function removeSocialLink(UserSocialLink $removeSocialLink)
{
    if ($this->socialLinks->contains($removeSocialLink)) {
        $this->socialLinks->removeElement($removeSocialLink);
        $this->socialLinks = clone $this->socialLinks;
    }
    return $this;
}

答案 1 :(得分:0)

让我们向您展示许多关系,例如Doctor and Skill: 一个医生可以有很多技能,而很多医生可以选择一种技能

您的所有者方

class Doctor implements DoctorInterface
{

     /**
     * @var Collection
     *
     * @ORM\ManyToMany(targetEntity="Skill", mappedBy="doctors", cascade={"persist"})
     */
    protected $skills;

     /**
     * Don't forget initial your collection property 
     * Doctor constructor.
     */
    public function __construct()
    {
        $this->skills = new ArrayCollection();
    }

        /**
     * @return Collection
     */
    public function getSkills(): Collection
    {
        return $this->skills;
    }

    /**
     * @param SkillInterface $skill
     */
    public function addSkill(SkillInterface $skill): void
    {
        if ($this->getSkills()->contains($skill)) {

            return;
        } else {

            $this->getSkills()->add($skill);
            $skill->addDoctor($this);
        }
    }

    /**
     * @param SkillInterface $skill
     */
    public function removeSkill(SkillInterface $skill): void
    {
        if (!$this->getSkills()->contains($skill)) {

            return;
        } else {

            $this->getSkills()->removeElement($skill);
            $skill->removeDoctor($this);
        }
    }
}

您的反面

class Skill implements SkillInterface
{   
     /**
     * @var Collection
     *
     * @ORM\ManyToMany(targetEntity="Doctor", inversedBy="skills", fetch="EXTRA_LAZY")
     */
    protecte    

    /**
     * Skill constructor.
     */
    public function __construct()
    {
        $this->doctors = new ArrayCollection();
    }

    /**
     * @return Collection
     */
    public function getDoctors(): Collection
    {
        return $this->doctors;
    }

    /**
     * @param DoctorInterface $doctor
     */
    public function addDoctor(DoctorInterface $doctor): void
    {
        if ($this->getDoctors()->contains($doctor)) {

            return;
        } else {

            $this->getDoctors()->add($doctor);
        }
    }

    /**
     * @param DoctorInterface $doctor
     */
    public function removeDoctor(DoctorInterface $doctor): void
    {
        if (!$this->getDoctors()->contains($doctor)) {

            return;
        } else {

            $this->getDoctors()->removeElement($doctor);
        }
    }
}