两个许多自我引用的集合 [英] two many2many self-referencing collection
问题描述
我使用FOSUserBundle,在我的用户类中,我需要两件事:
I use the FOSUserBundle and in my User class I want two things:
-
用户可以邀请其他用户进行友谊。朋友必须确认友谊。因此,用户有两个属性
$ inviteeForFriends
和$ inviteFriends
这是一个许多自我引用的协会来管理未决的友谊
a user can invite another user for friendship. The friend has to confirm that friendship. Therefor a user has two attributes
$inviteeForFriends
and$invitedFriends
which is a many2many self-referencing association to manage the pending friendships
如果友谊被确认,它将从等待的朋友中删除并添加到友谊中: $ myFriends
和 $ friendsWithMe
这也是一个很多自我参考的assoc。
if a friendship is confirmed it is removed from the pendingfriendship assoc and added to the friendship assoc: $myFriends
and $friendsWithMe
which is a many2many self-referencing assoc, too.
现在在控制器我这样做:
Now in a controller I do this:
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() - >包含($ friend)
最终会导致此错误:
注意:未定义的索引:$ inviteFriends在/home/r/workspace/MyProject/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php行764
A echo get_class($ user-> getInviteeForFriends())
echos a Doctrine\ORM\PersistentCollection
。
A echo $ user-> getInviteeForFriends() - > count();
导致相同的未定义索引错误。
A echo get_class($user->getInviteeForFriends())
echos a Doctrine\ORM\PersistentCollection
.
A echo $user->getInviteeForFriends()->count();
results in the same Undefined index error.
这些语句工作:
$user->getMyFriends()->contains($friend)
$user->getInvitedFriends()->contains($friend)
但$ $ user-> getInviteeForFriends() - >包含($ friend)
不。为什么?我在做什么错?
but the $user->getInviteeForFriends()->contains($friend)
does not. Why? What am I doing wrong?
这是User类的一部分:
This is a part of the User class:
/**
* @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
。没有美元符号。它不会在注释中。
In the annotations $inviteeForFriends
should be just inviteeForFriends
. No dollar symbol there. It is never in annotations.
另请参阅任何示例 from the doctrine orm 2.0 documentation :
/** @Entity */
class Feature
{
// ...
/**
* @ManyToOne(targetEntity="Product", inversedBy="features")
* @JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
// ...
}
这篇关于两个许多自我引用的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!