How to create M:N relation in Doctrine ORM
This article is about creating many-to-many relation for ORM framework Doctrine in ORM Designer.
This information is relevant only for older versions of ORM Designer. Since the version 1.3.x, ORM Designer directly supports M:N relations. Information how to use and create M:N relations you can find in the article describing the new version 1.3.1.
Introduce
Currently there isn't direct support for m:n relation in the core of ORM Designer. Nevertheless, there is a simple workaround to create it by ORM attributes. In this article we will show how to set-up the m:n relation in a simple ORM Doctrine project.
Example model
In this example we have following tables: (full YML file at the end of article)
Contact
ID: integer(PK)
Name: string(255)
Hobby
ID: integer(PK)
Name: string(255)
ContactHasHobby
Contact_ID: integer(FK)
Hobby_ID: integer(FK)
A model for this tables can look like this:
![]()
How to correctly define the m:n relation
This is a traditional way how to create m:n relation in erd. Now, we need to tell doctrine, that ContactHasBody is many-to-many relation table. In ORM Designer this can be done by using a special ORM Attribute for each foreign key relation.
If you want to have m:n relation from Contact to Hobby, please choose foreign key between Contact table and m:n table ContactHasHobby as shown on image.
![]()
In ORM properties panel find the attributes LinkTo and LinkToAlias. This attributes serves for m:n relation setting.
- LinkTo is a destination table for m:n relation
- LinkToAlias is a foreign object alias for m:n relation
In our example we select in "LinkTo" table choice Hobby and to "LinkToAlias" box we enter Hobbies.
![]()
After setting LinkTo and LinkToAlias for this foreign key, run model export to ORM Doctrine format. In YML file there is right defined m:n relation from Contact to Hobby (full YML file is at the end of article):
relations:
Hobbies:
class: Hobby
refClass: ContactHasHobby
local: Contact_ID
foreign: Hobby_ID
If you want to have an access also from Hobby to Contact table, simply select foreign key between Hobby and ContactHasBody. Than set as "LinkTo" item Contact and to "LinkToAlias" box enter Contacts (field LinkToAlias isn't required, you can leave it empty).
YML file for tables without m:n relation definiton:
Contact:
columns:
ID:
primary: true
type: integer
Name:
type: string(255)
Hobby:
columns:
ID:
primary: true
type: integer
Name:
type: string
ContactHasHobby:
columns:
Contact_ID:
primary: true
type: integer
Hobby_ID:
primary: true
type: integer
relations:
Contact:
local: Contact_ID
foreign: ID
Hobby:
local: Hobby_ID
foreign: ID
YML file for model with correctly defined m:n relation
Contact:
columns:
ID:
primary: true
type: integer
Name:
type: string(255)
relations:
Hobbies:
class: Hobby
refClass: ContactHasHobby
local: Contact_ID
foreign: Hobby_ID
Hobby:
columns:
ID:
primary: true
type: integer
Name:
type: string
ContactHasHobby:
columns:
Contact_ID:
primary: true
type: integer
Hobby_ID:
primary: true
type: integer
relations:
Contact:
local: Contact_ID
foreign: ID
Hobby:
local: Hobby_ID
foreign: ID
Short summary
I hope this article helped to clarify how to work with m:n relations in the ORM Designer. Our team is currently working on a new feature in ORM Designer which makes m:n relations definitions much easier.
Author: Ludek Vodicka


