Skip to Content

Symfony Doctrine Gotcha - Debugging m-m models

I recently came across a Symfony Doctrine gotcha which I couldn't find an answer from online. My problem was adding a new many to many relationship to an existing object and encountering the dreaded 'Unknown record property / related component' Symfony error.

The solution took a while to figure out. My object schema was this:

Resume:
...
  relations:
    ...
    Country:
      class: Country
      foreignAlias: Resumes
      local: home_country_id 
   InterestCountries:
      class: Country
      refClass: ResumeCountry
      foreignAlias: Resumes
   ...

InterestCountries was a many to many relationship between Resume and Country using a refclass of ResumeCounty. The problem was that I already had an existing one to many relationship between Resume and Country and that both relationships used the same foreignAlias. Thus, when Symfony generated the models, the foreignAlias for InterestCountries was not being written and this was causing the error in the form.

So, be very careful if you have more than one relationship with another object that you specify different foreignAlias's. A very simple idea but one which can cause a lot of problems if you aren't aware of it!

Comments