如何从多对多自引用关系的两个方面删除元素?

时间:2017-02-20 16:30:03

标签: php doctrine-orm symfony

我有这个实体在$ friendsWithMe和$ myFriends上有多对多的自引用:

/** @Entity */
class User
{
    // ...

    /**
     * Many Users have Many Users.
     * @ManyToMany(targetEntity="User", mappedBy="myFriends")
     */
    private $friendsWithMe;

    /**
     * Many Users have many Users.
     * @ManyToMany(targetEntity="User", inversedBy="friendsWithMe", cascade={"persist", "remove"})
     * @JoinTable(name="friends",
     *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="friend_user_id", referencedColumnName="id")}
     *      )
     */
    private $myFriends;

    public function __construct() {
        $this->friendsWithMe = new \Doctrine\Common\Collections\ArrayCollection();
        $this->myFriends = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

我有2个这样的功能:

    /**
     * Remove friendsWithMe
     *
     * @param \AppBundle\Entity\User $friendsWithMe
     */
    public function removeFriendsWithMe(\AppBundle\Entity\User $friendsWithMe)
    {
        $this->friendsWithMe->removeElement($friendsWithMe);
    }


/**
 * Remove myFriends
 *
 * @param \AppBundle\Entity\User $myFriends
 */
public function removeMyFriends(\AppBundle\Entity\User $myFriends)
{
    $this->myFriends->removeElement($myFriends);
}

removeFriensWithMe()正常工作,但 removeMyFriends()无法继续工作。

0 个答案:

没有答案