symfony2 orm条令中与额外字段的多个关系
嗨,我有一个和这里一样的问题:但我找不到答案:/我先尝试了很多次,然后在另一个网站上尝试了很多次。。。但是我不能用像这样的东西symfony2 orm条令中与额外字段的多个关系,symfony,orm,many-to-many,entity-relationship,Symfony,Orm,Many To Many,Entity Relationship,嗨,我有一个和这里一样的问题:但我找不到答案:/我先尝试了很多次,然后在另一个网站上尝试了很多次。。。但是我不能用像这样的东西 public function hasFriend(User $user) { return $this->myFriends->contains($user); } 因为在这个问题上有一些问题: This function is called, taking a User type $user variable and you the
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是一个请求的数组集合(与用户类型不同),来自关于contains()的条令文档:
那么,用额外的字段来解决这个众多关系的最佳方法是什么呢?或者,如果我要返回并设置onetomany和manytoone关系,如何修改hasFriend方法?例如,检查ID是否在ID的数组集合中
编辑:我有这张桌子。。。我需要的是:
1.选择我的朋友。。。还有我的追随者…看看我是否是他的朋友。(因为他可以和我成为朋友,而我不必和他在一起……就像在推特上一样)。我可以做很多,但我需要额外的字段,如“已浏览”“他订阅我的时间”,你可以在我的桌子上看到
然后像这样进行查询,然后能够在细枝中检查(app.user.hasFriend(follower)或类似的东西)
我试图与额外字段建立多对多关系,但也无法使其正常工作。。。我在论坛上读到的东西(不记得在哪里)是: 如果向关系中添加数据,那么它就不再是关系了。这是一个新的实体 这是正确的做法。使用新字段创建新实体,如果需要,创建自定义存储库以添加所需的方法 A B 将成为
A--一对多-->C(带有新字段)我正在添加另一个答案,因为它与我的原始答案无关。使用您发布的新信息,我将您发布的表/实体称为“关注者”。原始实体“用户” 如果创建以下关联,会发生什么情况:
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;
}
}
我不确定这是否会奏效,我真的没有太多的时间来测试它,但如果没有,我想它就在路上了。我使用两个关系,因为你不需要多对多。你需要引用一个用户可以有很多追随者,追随者可以跟随很多用户,但是由于“用户”表是同一个表,我做了两个关系,它们彼此无关,它们只是引用同一个实体,但用于不同的事情
试试看,然后实验会发生什么。你应该能够做到以下几点:
$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
是否有任何理由(除了在遵循关系时希望代码中只有一个->之外)不需要第三个实体?嗯。。。所以你说。。。我有一个用户实体,其中我将有一个朋友关系的friends属性到一个朋友实体,其中我将有一个与用户实体有多个关系的用户id。。。(正如我现在所做的)+我需要创建FriendsData实体,在那里我可以。。。什么?:/啊,太困惑了…请编辑这篇文章,告诉我们你想存储什么属性,以及存储在哪里,因为可能只需要两个实体就可以解决。我发布了另一个答案,因为它与此完全不同。好的,那么我如何从条令命令行工具生成第三个新实体?此命令:
app/console原则:映射:import-AppBundle-yml
仍然为原始两个表生成多个关系,只需忽略第三个表,而不是将其作为带有额外字段的实体进行整理:(
应该手动完成吗?
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