2013-03-04 129 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