与多个领域的多对多自我关系? [英] Many-to-many self relation with extra fields?
问题描述
我正在尝试开发一个朋友系统,我需要一个多对多的关系在我的用户实体;现在,这就是我所做的:
I am trying to develop a friends system, and I need a Many-To-Many relation on my User entities ; for now, this is what I've done :
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="friends")
*/
protected $friendsWith;
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="friendsWith")
* @JoinTable(name="friends",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
*/
protected $friends;
但是,我想为这些关系添加一些额外的字段,例如创建日期或状态(接受,待定,...);我创建了另一个实体朋友,我希望这个实体被用作朋友之间的链接。但是我真的不知道如何管理这个...
But I would like to have some extra fields for these relations, for example the creation date or the state (accepted, pending, ...) ; I've created another entity "Friend", and I would like this entity to be used as a link between friends. But I don't really know how to manage this...
你有什么想法吗?
谢谢!
推荐答案
恐怕你需要一个额外的课程来做这样的关联。
这是从理论文档的提示:
I'm afraid you need an extra class to make such an association. Here is the tip from doctrine documentation:
为什么多对多关联不太常见?因为你经常需要
将其他属性与一个关联相关联,其中
的例子介绍一个关联类。因此,直接
多对多关联消失,并被
替换为$ 3
类之间的一对多/多对一关联。
Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.
http://www.doctrine-project.org/docs/orm/2.1/en/reference/association-mapping.html#many-to-many-unidirectional
你和朋友在两个提交的$ myFriends和$ imFriendOf中的特殊类中相关:)
I guess it should be Friend -> Special Association Class (with fileds: user_id, friend_id, date created) ->Friend. And you associate Friend to special class in two filed $myFriends and $imFriendOf :)
这篇关于与多个领域的多对多自我关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!