Many-to-many self relation with extra fields?

前端 未结 1 869
日久生厌
日久生厌 2020-12-21 03:30

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         


        
相关标签:
1条回答
  • 2020-12-21 04:14

    I'm afraid you need an extra class to make such an association. Here is the tip from doctrine documentation:

    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

    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 :)

    0 讨论(0)
提交回复
热议问题