两个很多2自我引用的集合

时间:2011-06-12 00:04:42

标签: doctrine-orm symfony

我使用FOSUserBundle,在我的User类中我想要两件事:

  1. 用户可以邀请其他用户表示友谊。朋友必须确认友谊。因此,用户有两个属性$inviteeForFriends$invitedFriends,这是一个很多自我引用关联来管理待处理的友情

  2. 如果确认了友情,则会将其从pendingfriendship assoc中删除,并添加到友情关联:$myFriends$friendsWithMe,这也是一个很多自我引用的关联。

  3. 现在在控制器中我这样做:

    public function addAction() {
        $email = trim($this->get('request')->request->get("email"));
        // ...
    
        // find requested friend
        $userManager = $this->get('fos_user.user_manager');
        $friend = $userManager->findUserByEmail($email);
        // ...
        $user = $this->container->get('security.context')->getToken()->getUser();
    
        // ...
    
        // friend has not already asked the user
        if ($user->getInviteeForFriends()->contains($friend)) {
                throw new Exception(self::FRIEND_HAS_ALREADY_ASKED_MSG, self::FRIEND_HAS_ALREADY_ASKED);
        }
    }
    

    最后$user->getInviteeForFriends()->contains($friend)会导致此错误:

    Notice: Undefined index: $invitedFriends in /home/r/workspace/MyProject/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 764

    echo get_class($user->getInviteeForFriends())回复Doctrine\ORM\PersistentCollectionecho $user->getInviteeForFriends()->count();会导致相同的未定义索引错误。

    这些陈述起作用:

    $user->getMyFriends()->contains($friend)
    $user->getInvitedFriends()->contains($friend)
    

    $user->getInviteeForFriends()->contains($friend)没有。为什么?我做错了什么?

    这是User类的一部分:

    /**
     * @ORM\Entity
     * @ORM\Table(name="fos_user")
     */
    class User extends BaseUser
    {
    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="myFriends")
     */
    protected $friendsWithMe;
    
    /**
     * @ORM\ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
     * @ORM\JoinTable(name="friends",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="friend_user_id", referencedColumnName="id")}
     * )
     */
    protected $myFriends;
    
    
    /**
     * I am invited for a friendship from these friends.
     * 
     * @ORM\ManyToMany(targetEntity="User", mappedBy="$invitedFriends")
     */
    protected $inviteeForFriends;
    
    /**
     * I invited those friends for a friendship.
     * 
     * @ORM\ManyToMany(targetEntity="User", inversedBy="$inviteeForFriends")
     * @ORM\JoinTable(name="pending_friendships",
     *     joinColumns={@ORM\JoinColumn(name="inviter_user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="invitee_user_id", referencedColumnName="id")}
     * )
     */
    protected $invitedFriends;
    
    public function __construct()
    {
        parent::__construct();
        $this->friendsWithMe = new \Doctrine\Common\Collections\ArrayCollection();
        $this->myFriends = new \Doctrine\Common\Collections\ArrayCollection();
        $this->inviteeForFriends = new \Doctrine\Common\Collections\ArrayCollection();
        $this->invitedFriends = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    
    /**
     * Add friendsWithMe
     *
     * @param MyProject\SiteBundle\Entity\User $friendsWithMe
     */
    public function addFriendsWithMe(\MyProject\SiteBundle\Entity\User $friendsWithMe)
    {
        $this->friendsWithMe[] = $friendsWithMe;
    }
    
    /**
     * Get friendsWithMe
     *
     * @return Doctrine\Common\Collections\Collection $friendsWithMe
     */
    public function getFriendsWithMe()
    {
        return $this->friendsWithMe;
    }
    
    /**
     * Add myFriends
     *
     * @param MyProject\SiteBundle\Entity\User $myFriends
     */
    public function addMyFriends(\MyProject\SiteBundle\Entity\User $myFriends)
    {
        $this->myFriends[] = $myFriends;
    }
    
    /**
     * Get myFriends
     *
     * @return Doctrine\Common\Collections\Collection $myFriends
     */
    public function getMyFriends()
    {
        return $this->myFriends;
    }
    
    /**
     * Add inviteeForFriend
     *
     * @param MyProject\SiteBundle\Entity\User $inviteeForFriend
     */
    public function addInviteeForFriends(\MyProject\SiteBundle\Entity\User $inviteeForFriend)
    {
        $this->inviteeForFriends[] = $inviteeForFriend;
    }
    
    /**
     * Get inviteeForFriends
     *
     * @return Doctrine\Common\Collections\Collection $inviteeForFriends
     */
    public function getInviteeForFriends()
    {
        return $this->inviteeForFriends;
    }
    
    /**
     * Add invitedFriend
     *
     * @param MyProject\SiteBundle\Entity\User $invitedFriend
     */
    public function addInvitedFriends(\MyProject\SiteBundle\Entity\User $invitedFriend)
    {
        $this->invitedFriends[] = $invitedFriend;
    }
    
    /**
     * Get invitedFriends
     *
     * @return Doctrine\Common\Collections\Collection $invitedFriends
     */
    public function getInvitedFriends()
    {
        return $this->invitedFriends;
    }
    }
    

1 个答案:

答案 0 :(得分:2)

在注释中$inviteeForFriends应该只是inviteeForFriends。那里没有美元符号。它永远不会在注释中。

另见from the doctrine orm 2.0 documentation示例:

/** @Entity */
class Feature
{
    // ...
    /**
     * @ManyToOne(targetEntity="Product", inversedBy="features")
     * @JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $product;
    // ...
}