Doctrine - database relation - many-to-many add status
-
13-07-2021 - |
Question
How do I implement the following scenario the best way in ORM (Doctrine)?
Scenario: A user can belong to one or many teams. A team can consist of one or many users.
So, the first part is no problem, but I want the user to have an "activation_status" for every team he belongs.
So as I'm coming from SQL and relational databases i could add an "activation_status" field in my many-to-many relationship table "team2user"
-> database fields: team_id, user_id, activation_status
But how can I implement that with Doctrine? I persist the entities team and user. But how do I persist the activation_status correctly?
Many thanks in advance.
Solution
I think you could revise your entities and add an Activation Entity, that way your entities would be like this:
- User has several Activations (OneToMany)
- Activation belongs to one user (ManyToOne), and belongs to a Team (ManyToOne)
- Team has several Activations (OneToMany)
You can also define ManyToMany relationship between User and Team through Activation entity.
OTHER TIPS
The only way to achieve this (at least up to Doctrine 2.1, I'm not 100% sure how it looks in higher versions) is create separate entity TeamUser
and relate it with User
and Team
entities using ManyToOne
relation. Then you can put whatever you want inside your new entity (for your example it will be additional $activationStatus
property)