Michael
Michael

Reputation: 4876

Doctrine - using the same table for multiple (many-to-many) relations

Say I have three entities: Page, Product and Media.

Well, I would like to have this realations:

Page <-(many-to-many)-> Media

Product <-(many-to-many)-> Media

Using the common approach for solving this, it would result two tables that look very similar.

My question is: Can I use a single table for both relations using Doctrine?

What I only need is a way of suggesting a new column that would indicate if the counterpart of media on a particular row is Product or Page.

Upvotes: 4

Views: 1225

Answers (1)

M. Foti
M. Foti

Reputation: 3182

The answer is No. Relations table N:N must have unique table name. But... You can aways create a structure of that kind:

Page <--1:N--> MyGreatJoinTable <--N:1--> Media
Product <--1:N--> MyGreatJoinTable <--N:1--> Media

You don't need to describe the reference because it refers to a different table, but if you want you can do it.

Of course MyGreatJoinTable will have at least three columns:

- PageId
- ProductId
- MediaId

Upvotes: 3

Related Questions