1.4.6 Build 488

visual editor for ORM frameworks

Pricing information Free download

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

ORM Designer on Twitter

Feed from our blog

Missing field-type icons in ORM Designer

Propel ORM Behaviors support!

Propel ORM validators support!

How to change export format (schema.yml / schema.xml)

How to import model from MySql Workbench

New visual symbols for NULL/NOT NULL column types

Execute another application from ORM Designer import/export script

Forum topics

ORM Designer forum: http://forum.orm-designer.com/

About ORM Designer

ORM Designer is a tool for modelling entity relationship diagrams (ERD). The biggest advantage is full support of any ORM framework which has its definitions stored in a markup language or other structured file.

Currently supported ORMs are PHP Doctrine, PHP Propel and CakePHP frameworks, but more will be added in close future.

Inventic s.r.o. - PayPal verified

Real Time Web Analytics

Clicky

Legal notice: The name CakePHP and the cake icon are trademarks of the Cake Software Foundation Inc., and used with permission. "symfony" is a trademark of Fabien Potencier.

© 2009-2012 Inventic s.r.o. all rights reserved