嗨,我有与这里相同的问题:与额外字段的多对多自关系? https://stackoverflow.com/questions/9134856/many-to-many-self-relation-with-extra-fields但我找不到答案:/我首先尝试了ManyToOne,然后在另一个网站OneToMany ...但后来我无法使用类似的东西
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.
$this->myFriends 是一个请求的 ArrayCollection(与 User 的类型不同),来自关于 contains() 的学说文档:
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.选择我的朋友...和我的关注者...检查我是否是他的朋友。 (因为他可以和我成为朋友,而我不必和他在一起......就像在推特上一样)。我可以做很多,但我需要额外的字段,例如:“查看”“他订阅我的时间”,正如您在我的桌子上看到的那样。
并像这样进行查询,然后能够在树枝中检查 if (app.user.hasFriend(follower) 或类似的东西)
$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();