Doctrine Many To Many with Extra Fields

Extra fields for Many to Many relationship is usually well supported. However in Doctrine this is actually something that you can’t do with just normal ManyToMany entity.

In Doctrine you have to define three Entities where you kind of manually create the Many To Many relationship. This is done like this.

Lets say we have Products and Categories. Products can have many Categories and Categories can have many Products. So we need a many to many relationship.

Now for the sake of example we also need quantity of how many products are in a category. It would be convenient to store the information into the middle table of our many to many relationship.

Lets first define the Product class

So you see that we are using OneToMany relationship in here and it is targeting Entity CategoryProducts which eventually will be our middle table.

Now lets define Category class:

Now we have defined both of our main entities. Now it’s time for the middle table.

Here in the middle table we just define ManyToOne side relationships to their corresponding OneToMany relations on the main entities. We can also add here as many extra fields as we like. Currently this entity contains the Quantity which will store the information of how many products are in this category.