symfony - ManyToMany relationship with extra fields in symfony2 orm doctrine -


hi have same question here: many-to-many self relation fields? cant find answer :/ tried first manytoone , @ other site onetomany ... not use like

    public function hasfriend(user $user) {     return $this->myfriends->contains($user); }   

because there problem:

this function called, taking user type $user variable , use contains()      function on $this->myfriends. 

$this->myfriends arraycollection of requests (so different type user) , doctrine documentation contains():

the comparison of 2 elements strict, means not value type must match. 

so best way solve manytomany relationship fields? or if go , set onetomany , manytoone relationship how can modify hasfriend method? example check if id in array collection of id's.

edit: have table... , need is: 1. select friends... , followers ...check if friend him or not. (because can friend me , dont have him... on twitter). make manytomany need fields like: "viewed" "time when subscribe me" can see @ table.

and make query , able in twig check if (app.user.hasfriend(follower) or that)

           $qb = $this->createquerybuilder('r')                   ->select('u')                   ->innerjoin('userbundle:user', 'u')                   ->where('r.friend_id=:id')                   ->setparameter('id', $id)                   ->orderby('r.time', 'desc')                   ->setmaxresults(50);      return $qb->getquery()               ->getresult(); 

enter image description here

i'm adding answer since has nothing original answer. using new info posted, i'm calling table/entity posted "follower". original entity, "user".

what happens if create following associations:

 namespace acme\userbundle\entity;  use doctrine\orm\mapping orm;  /**  * acme\userbundle\entity\user  *  * @orm\table()  * @orm\entity  */ class user {     /**      * @var integer $id      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @orm\onetomany(targetentity="acme\followerbundle\entity\follower", mappedby="followeduser")      */     protected $followers;      /**      * @orm\onetomany(targetentity="acme\followerbundle\entity\follower", mappedby="followeeuser")      */     protected $followees;      /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }     public function __construct()     {         $this->followers = new \doctrine\common\collections\arraycollection();     $this->followees = new \doctrine\common\collections\arraycollection();     }      /**      * add followers      *      * @param acme\followerbundle\entity\follower $follower      */     public function addfollower(\acme\followerbundle\entity\follower $follower)     {         $this->followers[] = $follower;     }      /**      * add followees      *      * @param acme\followerbundle\entity\follower $followee      */     public function addfollowee(\acme\followerbundle\entity\follower $followee)     {         $this->followees[] = $followee;     }          /**      * followers      *      * @return doctrine\common\collections\collection       */     public function getfollowers()     {         return $this->followers;     }      /**      * followees      *      * @return doctrine\common\collections\collection       */     public function getfollowees()     {         return $this->followees;     } }  
 namespace acme\followerbundle\entity;  use doctrine\orm\mapping orm;  /**  * acme\followerbundle\entity\follower  *  * @orm\table()  * @orm\entity  */ class follower {     /**      * @var integer $id      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @orm\manytoone(targetentity="acme\userbundle\entity\user", inversedby="followers")      * @orm\joincolumn(name="user_id", referencedcolumnname="id")      */     protected $followeduser;      /**      * @orm\manytoone(targetentity="acme\userbundle\entity\user", inversedby="followees")      * @orm\joincolumn(name="followee_id", referencedcolumnname="id")      */     protected $followeeuser;      /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set followeduser      *      * @param acme\userbundle\entity\user $followeduser      */     public function setfolloweduser(\acme\userbundle\entity\user $followeduser)     {         $this->followeduser = $followeduser;     }      /**      * followeduser      *      * @return acme\userbundle\entity\user       */     public function getfolloweduser()     {         return $this->followeduser;     }      /**      * set followeeuser      *      * @param acme\userbundle\entity\user $followeeuser      */     public function setfolloweeuser(\acme\userbundle\entity\user $followeeuser)     {         $this->followeeuser = $followeeuser;     }      /**      * followeeuser      *      * @return acme\userbundle\entity\user       */     public function getfolloweeuser()     {         return $this->followeeuser;     } }  

i'm not sure if trick, don't have time test it, if doesn't, thnk it's on it's way. i'm using 2 relations, because don't need many many. need reference user can have lot of followers, , follower can follow lot of users, since "user" table same one, did 2 relations, have nothing eachother, reference same entity different things.

try , experiment happens. should able things like:

 $user->getfollowers();  $follower->getfolloweduser();  , check if user being followed follower user_id equals $userthatiwanttocheck  , search in followers follower user = $user , followeduser=$possiblefriend  

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -