Search code examples
symfonyormdoctrinemany-to-manyrelational

Doctrine - database relation - many-to-many add status


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:

    1. User has several Activations (OneToMany)
    2. Activation belongs to one user (ManyToOne), and belongs to a Team (ManyToOne)
    3. Team has several Activations (OneToMany)

    You can also define ManyToMany relationship between User and Team through Activation entity.