How to handle additional columns in join tables when using Symfony?

风流意气都作罢 提交于 2019-12-07 03:27:03

问题


Let's assume I have two Entities in my Symfony2 bundle, User and Group. Associated by a many-to-many relationship.

    ┌────────────────┐         ┌────────────────┐         ┌────────────────┐
    |      USER      |         | USER_GROUP_REL |         |     GROUP      |
    ├────────────────┤         ├────────────────┤         ├────────────────┤
    | id#            ├---------┤ user_id#       |    ┌----┤ id#            |
    | username       |         | group_id#      ├----┘    | groupname      |
    | email          |         | created_date   |         |                |
    └────────────────┘         └────────────────┘         └────────────────┘

What would be a good practice or a good approach to add additional columns to the join table, like a created date which represents the date when User joined Group?

I know that I could use the QueryBuilder to write an INSERT statement.

But as far as I have not seen any INSERT example of QueryBuilder or native SQL which makes me believe that ORM/Doctrine try to avoid direct INSERT statements (e.g. for security reasons). Plus as far as I have understood Symfony and Doctrine I would be taken aback if such a common requirement wouldn't be covered by the framework.


回答1:


You want to set a property of the relation. This is how it's done in doctrine: doctrine 2 many to many (Products - Categories)

I answered that question with a use case (like yours).

This is an additional question / answer which considers the benefits and use cases: Doctrine 2 : Best way to manage many-to-many associations



来源:https://stackoverflow.com/questions/18160528/how-to-handle-additional-columns-in-join-tables-when-using-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!