与额外字段的多对多自我关系? [英] Many-to-many self relation with extra fields?

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

问题描述

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

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 :

/**
 * @ORMManyToMany(targetEntity="User", mappedBy="friends")
 */
protected $friendsWith;

/**
 * @ORMManyToMany(targetEntity="User", inversedBy="friendsWith")
 * @JoinTable(name="friends",
 *            joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
 *            inverseJoinColumns={@JoinColumn(name="friend_user_id", referencedColumnName="id")}
 *           )
 */
protected $friends;

但我想为这些关系添加一些额外的字段,例如创建日期或状态 (accepted, pending, ...) ;我创建了另一个实体朋友",我希望这个实体被用作朋友之间的链接.但我真的不知道如何管理这个...

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

我想应该是 Friend -> Special Association Class (with fields: user_id,friend_id, date created) ->Friend.并且您将 Friend 与两个归档的 $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天全站免登陆