2

I am implementing a system with two entities: Product and Category. I want to create a many-to-many relations between these two entities but I don't want to use the many-to-many option in Doctrine. I want to be able to create the middle table (product-category) by myself so that later I can access this table as well. How should I define the relationships between these three tables? (Considering that the relationship between product and category is many-to-many in my project). Simple demonstrative examples with doctrine ORM annotations is much appreciated so I don't get this wrong. Many thanks. What is the best practice?

Nesa Mouzehkesh
  • 3,755
  • 2
  • 14
  • 11

1 Answers1

4

You can add a middle entity called, for example, "ProductCategory". Then you create a one-to-many relationship between "Product" and "ProductCategory" and another one-to-many relationship between "Category" and "ProductCategory".

This solution even allows you to add some extra fields to "ProductCategory" table.

Product [1]------>[n] ProductCategory [n]<-------[1] Category

Max Leske
  • 5,007
  • 6
  • 42
  • 54
Thomas Shelby
  • 1,340
  • 3
  • 20
  • 39