two many2many self-referencing collection
I use the FOSUserBundle and in my User class I want two开发者_如何学Python things:
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 friendshipsif 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);
}
}
The $user->getInviteeForFriends()->contains($friend)
in the end results in this error:
Notice: Undefined index: $invitedFriends in /home/r/workspace/MyProject/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 764
A echo get_class($user->getInviteeForFriends())
echos a Doctrine\ORM\PersistentCollection
.
A echo $user->getInviteeForFriends()->count();
results in the same Undefined index error.
Those statements work:
$user->getMyFriends()->contains($friend)
$user->getInvitedFriends()->contains($friend)
but the $user->getInviteeForFriends()->contains($friend)
does not. Why? What am I doing wrong?
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;
}
}
In the annotations $inviteeForFriends
should be just inviteeForFriends
. No dollar symbol there. It is never in annotations.
Also see any examples from the doctrine orm 2.0 documentation:
/** @Entity */
class Feature
{
// ...
/**
* @ManyToOne(targetEntity="Product", inversedBy="features")
* @JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
// ...
}
精彩评论