我使用 FOSUserBundle,在我的用户类中我想要两件事:
一个用户可以邀请另一个用户建立友谊。 friend 必须确认这种友谊。因此,用户有两个属性
$inviteeForFriends
和$invitedFriends
,这是一个管理未决友谊的多对多自引用关联如果友谊得到确认,它将从 pendingfriendship assoc 中删除并添加到友谊 assoc 中:
$myFriends
和$friendsWithMe
这是一个 many2many self-也引用 assoc。
现在在 Controller 中我这样做:
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)
导致了这个错误:
注意:未定义索引:/home/r/workspace/MyProject/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 第 764 行中的 $invitedFriends
echo get_class($user->getInviteeForFriends())
回显 Doctrine\ORM\PersistentCollection
。
echo $user->getInviteeForFriends()->count();
导致相同的未定义索引错误。
这些语句有效:
$user->getMyFriends()->contains($friend)
$user->getInvitedFriends()->contains($friend)
但是 $user->getInviteeForFriends()->contains($friend)
没有。为什么?我做错了什么?
这是用户类的一部分:
/**
* @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;
}
}
最佳答案
在注释中 $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;
// ...
}
关于doctrine-orm - 两个 many2many 自引用集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6319287/