嗨額外的字段多對多關係,我有同樣的問題在這裏: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();
是否有任何理由(不是想其他只有一個 - 以下的關係時>在你的代碼),你不希望第三實體? – MrGlass 2013-03-04 16:05:52
hm ...所以你說...我有用戶實體,我將有朋友財產與OneToMany關係的朋友實體,我會有與UserEntity ManyToOne關係的user_id ...(如我現在)+我需要創建FriendsData實體,我將擁有...什麼? :/啊這麼困惑... – EnchanterIO 2013-03-04 16:15:48
請編輯帖子,並告訴我們什麼屬性,你想存儲和在哪裏,因爲也許它可以解決使用只有兩個實體 – gdlin 2013-03-04 20:28:30