Search code examples
mysqlsymfonymany-to-manyentity

Symfony 6 - Many to Many Relationship - Storage of Data in Linking Table?


I am currently working on a symfony 6 project with a mysql database and have the following issue:

My situation: I have two tables/entities:

  1. Orders
  2. Article

This two entities are related in a many to many relationship. So one order can have many articles and one article can belong to many orders.

This relationship leads to the creation of a third table that links the other two tables in terms of the many to many relationship.

The Problem: I am now wondering where to store the information about which Article gets ordered how often.

My Idea: I thought about storing this information in the linking table. However normally Symfony does not create an Entity for this. Therefore I think that there might be a better solution.

Does anybody have Idea where to store this information? :)


Solution

  • Thanks to @Cerad I found out, that when there is the need for storing such extra information, a Many To Many Relationship is not the right answer.

    In a case like this, it makes sense to create a further Entity like OrderArticle. This Entity is in a many to one relation with both Orders and Articles.

    As also stated in the Symfonycasts Tutorial here: https://symfonycasts.com/screencast/symfony4-doctrine-relations/many-to-many-joins

    Maybe this helps someone in the future as well. :)