How do I correctly implement a ‘friends relationship’ with Doctrine

I have a symfony app whereby a user can send a friend request, which will be received by the other user whom can then accept or decline the request thereby creating the relationship. I have an idea of how to implement this, however my inexperience has lead me to a wall.

Friends Entity

/**
 * @ORMEntity()
 */
class Friends
{
    const STATUS_PENDING = 'STATUS_PENDING';
    const STATUS_FRIEND = 'STATUS_FRIEND';
    const STATUS_BLOCKED = 'STATUS_BLOCKED';
    const DEFAULT_STATUS = [self::STATUS_PENDING];

    /**
     * @ORMManyToOne(targetEntity="User", inversedBy="myFriends")
     * @ORMId
     */
    private $user;

    /**
     * @ORMManyToOne(targetEntity="User", inversedBy="friendsWithMe")
     * @ORMId
     */
    private $friend;

    /**
     * @ORMColumn(type="simple_array")
     */
    private $status;

    public function __construct()
    {
        $this->status = self::DEFAULT_STATUS;
    }

    public function addFriend(User $user, User $friend): self
    {

         $userID = $user->getId();
         $friendID = $friend->getId();


        switch ($userID)
        {
            case $userID > $friendID:
                $this->setUser($friend);
                $this->setFriend($user);
                break;
            case $userID < $friendID:
                $this->setUser($user);
                $this->setFriend($friend);
                break;
            case $userID ===  $friendID:
                break;
        }

        return $this;
    }


}

User Entity

/**
 * @ORMEntity()
 */
class User implements UserInterface
{
     /**
     * Many Users have Many Users.
     * The people who I think are my friends.
     *
     * @ManyToMany(targetEntity="User", mappedBy="myFriends")
     */
    private $friendsWithMe;

    /**
     * Many Users have many Users.
     * The people who think that I’m their friend.
     *
     * @ORMOneToMany(targetEntity="Friends", mappedBy="user")
     */
    private $myFriends;

    public function __construct()
    {
        $this->roles = self::DEFAULT_ROLES;
        $this->households = new ArrayCollection();
        $this->myFriends = new ArrayCollection();
        $this->friendsWithMe = new ArrayCollection();
    }



}

The Controller looks like this:

    private $entityManager;

    private $friends;

    private $user;

    public function __construct(TokenStorageInterface $tokenStorage, EntityManagerInterface $entityManager)
    {
        $this->user = $tokenStorage->getToken()->getUser();
        $this->friends = new Friends();
        $this->entityManager = $entityManager;
    }

    /**
     * @Route("/api/addFriend/{id}", name="addFriend", requirements={"id"="d+"}, methods={"POST"})
     * @ParamConverter("user", class="App:User")
     */
    public function addFriend(User $user)
    {
        $loggedInUser = $this->user;
        $add = $this->friends->addFriend($loggedInUser,$user);
        $this->entityManager->persist($this->friends);
        $this->entityManager->flush();
    }

The issue is that When I try to use the controller. It fails at persist() and and the error reads:

Warning: get_class() expects parameter 1 to be object, int given

I believe the issue lies either with the way i am using the user objects in the Friends class, Or the way the friends entity is made. I followed This to show me how to implement the many to many user/Friends relationship initially. Could anyone help guide me in the right direction please? Thank you very much.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *