Symfony Doctrine Entity Friend hasFriend followers

I need to set a follower by following the (myFriends) hasFriend logic for my project. The column "odobera" means "next" for example nick (id user) odobera (next to this user). User (25) is the next user (37).

Query table:

enter image description here

User entity: /** * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="odobera") */ protected $followers; /** * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="nick") */ protected $myFriends; public function __construct() { parent::__construct(); $this->followers = new \Doctrine\Common\Collections\ArrayCollection(); $this->myFriends = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get myFriends * * @return \Doctrine\Common\Collections\Collection */ public function getMyFriends() { return $this->myFriends; } /** * * @param \TB\UserBundle\Entity\User $user * @return bool */ public function hasFriend(User $user) { return $this->myFriends->contains($user); } class Requests { /** * @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="myFriends") * @ORM\JoinColumn(name="nick", referencedColumnName="id") */ protected $nick; /** * @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User",inversedBy="followers") * @ORM\JoinColumn(name="odobera", referencedColumnName="id") */ protected $odobera; 

In the controller:

  $myFollowers=$user->getMyFriends(); 

returns:

enter image description here

What's good: returns 1 record. I actually follow one person, as you can see here the record identifier is 24395

Database query table:

enter image description here

I don’t know if it’s good that the getMyFriends function returns a response in this “format”. Please study it carefully.

Then I select the followers from the query and in the loop:

 {% for follower in followers %} and i print data like this (works greate) {{ follower.nick }} or if i want some fields from user entity {{ follower.nick.rank }} {% endfor %} 

But the problem is here:

  {% if (app.user.hasFriend(follower.nick)) %} 

What returns false, why? I follow this user as I checked in the controller with a dump: P A few lines.

+2
source share
1 answer

The problem is that you are comparing two different type variables.

When you do this: {% if (app.user.hasFriend(follower.nick)) %} following function is called:

 /** * * @param \TB\UserBundle\Entity\User $user * @return bool */ public function hasFriend(User $user) { return $this->myFriends->contains($user); } 

This function is called by accepting the variable User type $user , and then you use the contains() function on $this->myFriends .
$this->myFriends is an ArrayCollection of Requests (so that it differs from User ) and from the documentation for the doctrine of contains() :

The comparison of the two elements is strict, it means not only the value, but also the type must match.

http://www.doctrine-project.org/api/common/2.1/class-Doctrine.Common.Collections.ArrayCollection.html

0
source

All Articles