与Symfony2 Orm准则中的其他字段的ManyToMany关系 [英] ManyToMany relationship with extra fields in symfony2 orm doctrine
问题描述
您好,我在这里也遇到了同样的问题: Many-to-许多自我关系与额外字段有关?但我找不到答案:/我先尝试了ManyToOne,然后尝试了另一个站点OneToMany ...,但是后来我无法使用类似的东西
Hi i have that same question as here: Many-to-many self relation with extra fields? but i cant find an answer :/ I tried first ManyToOne and at the other site OneToMany ... but then i could not use something like
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()的原则文档:
$this->myFriends is an ArrayCollection of Requests (so different type than User) and from the doctrine documentation about contains():
The comparison of two elements is strict, that means not only the value but also the type must match.
那么解决带有额外字段的ManyToMany关系的最佳方法是什么?或者,如果我返回并设置单音和多音之间的关系,该如何修改hasFriend方法?例如,检查ID是否在ID的数组集合中.
So what is the best way to solve this ManyToMany relationship with extra fields? Or if i would go back and set the onetomany and manytoone relationship how can i modify the hasFriend method? To example check if ID is in array collection of ID's.
我有这张桌子...,我需要的是: 1.选择我的朋友...和我的追随者...检查我是否与他成为朋友. (因为他可以和我成为朋友,而我不必和他在一起……就像在Twitter上一样).我可以说很多话,但是我需要一些额外的字段,例如:查看",他订阅我的时间",如您在我的桌子上看到的那样.
i have this table... and what i need is: 1. select my friends... and my followers ...check if i am friend with him or not. (because he can be friend with me and i dont have to be with him... like on twitter). I could make manytomany but i need extra fields like: "viewed" "time when he subscribe me" as you can see at my table.
进行这样的查询,然后可以在树枝中检查(app.user.hasFriend(follower)还是类似的东西)
And make query like this and then be able in twig check if (app.user.hasFriend(follower) or something like 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();
推荐答案
我要添加另一个答案,因为它与我的原始答案无关.使用您发布的新信息,我将您发布的表/实体称为关注者".原始实体用户".
I'm adding another answer since it has nothing to do with my original answer. Using the new info you posted, I'm calling the table/entity you posted "Follower". The original entity, "User".
如果创建以下关联会发生什么:
What happens if you create the following associations:
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"表是同一个人,因此我做了两个关系,它们与彼此无关,它们只是引用相同的实体,但要引用不同的内容.
I'm not sure if this would do the trick, I really don't have much time to test it, but if it doesn't, I thnk that it's on it's way. I'm using two relations, because you don't need a many to many. You need to reference that a user can have a lot of followers, and a follower can follow a lot of users, but since the "user" table is the same one, I did two relations, they have nothing to do with eachother, they just reference the same entity but for different things.
尝试一下,尝试一下会发生什么.您应该能够执行以下操作:
Try that and experiment what happens. You should be able to do things like:
$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
这篇关于与Symfony2 Orm准则中的其他字段的ManyToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!