与多个领域的多对多自我关系? [英] Many-to-many self relation with extra fields?

查看:94
本文介绍了与多个领域的多对多自我关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个朋友系统,我需要一个多对多的关系在我的用户实体;现在,这就是我所做的:

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我想这应该是朋友 - >特殊协会类(带有档案:user_id,friend_id,创建日期) - >朋友

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆