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

Suppose I have two entities in my Symfony2 package, User and Group . Associated with a many-to-many relationship.

  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” | USER | | USER_GROUP_REL | | GROUP | โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค | id# โ”œ---------โ”ค user_id# | โ”Œ----โ”ค id# | | username | | group_id# โ”œ----โ”˜ | groupname | | email | | created_date | | | โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 

What would be good practice or a good approach to add additional columns to the join table, for example, to the creation date, which represents the date that User joined the Group ?

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

But as far as I have not seen any example of INSERT QueryBuilder or native SQL, which leads me to believe that ORM / Doctrine try to avoid direct INSERT statements (for example, for security reasons). Plus, as I understood Symfony and Doctrine, I would be stunned if such a general requirement were not covered by the frame.

+6
source share
1 answer

You want to set the relationship property. This is how it is done in the doctrine: doctrine 2 is many for many (Products - Categories)

I answered this question if used (like yours).

This is an additional question / answer that discusses the benefits and uses: Doctrine 2: the best way to manage many-to-many associations

+2
source

Source: https://habr.com/ru/post/951450/


All Articles