I am using FOSUserBundle, and in my User class I want two things:
user can invite another user for friendship. A friend must confirm this friendship. Thus, the user has two attributes, $inviteeForFriendsand $invitedFriends, which is a large association of self-references for managing pending friendships
if a friendship is confirmed, it is removed from the associated partner and added to the friendly partner: $myFriendsand $friendsWithMe, which is also a multi-mass assistant.
Now in the controller I do this:
public function addAction() {
$email = trim($this->get('request')->request->get("email"));
$userManager = $this->get('fos_user.user_manager');
$friend = $userManager->findUserByEmail($email);
$user = $this->container->get('security.context')->getToken()->getUser();
if ($user->getInviteeForFriends()->contains($friend)) {
throw new Exception(self::FRIEND_HAS_ALREADY_ASKED_MSG, self::FRIEND_HAS_ALREADY_ASKED);
}
}
This ultimately $user->getInviteeForFriends()->contains($friend)leads to 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.
These statements work:
$user->getMyFriends()->contains($friend)
$user->getInvitedFriends()->contains($friend)
$user->getInviteeForFriends()->contains($friend) . ? ?
User:
class User extends BaseUser
{
protected $friendsWithMe;
protected $myFriends;
protected $inviteeForFriends;
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();
}
public function addFriendsWithMe(\MyProject\SiteBundle\Entity\User $friendsWithMe)
{
$this->friendsWithMe[] = $friendsWithMe;
}
public function getFriendsWithMe()
{
return $this->friendsWithMe;
}
public function addMyFriends(\MyProject\SiteBundle\Entity\User $myFriends)
{
$this->myFriends[] = $myFriends;
}
public function getMyFriends()
{
return $this->myFriends;
}
public function addInviteeForFriends(\MyProject\SiteBundle\Entity\User $inviteeForFriend)
{
$this->inviteeForFriends[] = $inviteeForFriend;
}
public function getInviteeForFriends()
{
return $this->inviteeForFriends;
}
public function addInvitedFriends(\MyProject\SiteBundle\Entity\User $invitedFriend)
{
$this->invitedFriends[] = $invitedFriend;
}
public function getInvitedFriends()
{
return $this->invitedFriends;
}
}