2013-03-04 138 views
0

嗨额外的字段多对多关系,我有同样的问题在这里:Many-to-many self relation with extra fields?但我不能找到一个答案:/我第一次尝试多对一和其他站点的一对多......但后来我不能使用的东西像与Symfony2的ORM学说

public function hasFriend(User $user) 
{ 
    return $this->myFriends->contains($user); 
} 

,因为有一些这样的问题:

This function is called, taking a User type $user variable and you then use the contains()  function on $this->myFriends. 

$这个 - > myFriends是请求的ArrayCollection(所以不同的类型的用户),并从理论文献约含有():

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

那么,什么是解决与额外的字段这个多对多关系的最佳方式?或者,如果我会回去,并设置onetomany和manytoone关系如何修改hasFriend方法?例如检查ID是否在ID的数组集合中。

编辑:我有这个表...和我需要的是:1。 选择我的朋友......和我的追随者...检查,如果我的朋友与他或不。 (因为他可以成为我的朋友,我不必和他在一起......就像在twitter上)。我可以做很多东西,但我需要额外的领域,如:“看到”,“他订阅我的时间”,你可以在我的桌子上看到。

并作出这样的查询,然后可以在树枝检查(app.user.hasFriend(跟随)或类似的东西)

  $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

回答

3

我加入另一个答案,因为它无关,与我原来的答复。使用您发布的新信息,我打电话给您发布“追随者”的表/实体。原始实体“用户”。

如果您创建以下关联,会发生什么:


namespace Acme\UserBundle\Entity; 

use Doctrine\ORM\Mapping as 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; 

    /** 
    * Get 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; 
    }  

    /** 
    * Get followers 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getFollowers() 
    { 
     return $this->followers; 
    } 

    /** 
    * Get followees 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getFollowees() 
    { 
     return $this->followees; 
    } 
} 


namespace Acme\FollowerBundle\Entity; 

use Doctrine\ORM\Mapping as 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; 

    /** 
    * Get 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; 
    } 

    /** 
    * Get 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; 
    } 

    /** 
    * Get followeeuser 
    * 
    * @return Acme\UserBundle\Entity\User 
    */ 
    public function getFolloweeuser() 
    { 
     return $this->followeeuser; 
    } 
} 

我不知道这是否会做的伎俩,我真的没有太多的时间来测试它,但如果它不,我认为它已经到了。我使用两种关系,因为你不需要多对多的关系。你需要引用一个用户可以有很多追随者,一个追随者可以追随很多用户,但由于“用户”表是相同的,我做了两个关系,他们与彼此没有任何关系,他们只是参考相同的实体,但针对不同的事情。

尝试一下并试验会发生什么。你应该能够做这样的事情:


$user->getFollowers(); 

$follower->getFollowedUser(); 

and you could then check if a user is being followed by a follower whose user_id equals $userThatIwantToCheck 

and you could search in Followers for a Follower whose user = $user and followeduser=$possibleFriend 
 
5

我想有很多与多余的领域的许多关系,并不能使其工作...我在一个论坛(不记得在哪里)阅读的东西是:

如果你添加数据的关系,那么它是不是一个关系ymore。这是一个新的实体。

而且它是做正确的事。用新字段创建一个新实体,如果需要,可以创建一个自定义存储库来添加所需的方法。

一个< ---多对多与现场--->乙

将成为

一个 - 资助了许多 - > C(用新的领域)< - 一对多--B

,当然还有,C与A和B都

关系多对一

我到处找关于如何做到这一点,但最后,它是做正确的事情,如果你添加数据,它不再是一种关系。

您也可以复制的内容通常包含做,或尝试将其覆盖在自定义库中,做任何你需要做的。

我希望这会有所帮助。

+0

是否有任何理由(不是想其他只有一个 - 以下的关系时>在你的代码),你不希望第三实体? – MrGlass 2013-03-04 16:05:52

+0

hm ...所以你说...我有用户实体,我将有朋友财产与OneToMany关系的朋友实体,我会有与UserEntity ManyToOne关系的user_id ...(如我现在)+我需要创建FriendsData实体,我将拥有...什么? :/啊这么困惑... – EnchanterIO 2013-03-04 16:15:48

+0

请编辑帖子,并告诉我们什么属性,你想存储和在哪里,因为也许它可以解决使用只有两个实体 – gdlin 2013-03-04 20:28:30