Mastering Doctrine Relationships in Symfony

In the part 1 of the Doctrine Tutorial we got a sweet setup with Doctrine: entities, queries, migrations and fixtures.

But, we left out one big piece: database relations. Once you've mastered these, you'll be unstoppable. Unfortunately, a lot of information out there make Doctrine relations look over-complicated. Actually, they're simple and beautiful. Sure, they have some pitfalls - but they'll also help you build a complex data structure in no time.

  • ManyToOne relationships (associations)
  • The OneToMany side of a relation
  • The downfall of owning versus inverse sides of a relation
  • Doing magic with the ArrayCollection
  • Querying with Joins

... and more - like some magic with the "Param Conversion".

Your Guides

Ryan Weaver Leanna Pelham

Questions? Conversation?

  • 2016-09-20 weaverryan

    I just started on the tutorial today :). But, that still means that it's anywhere from 3-6 weeks away from the release schedule. So, not too long now :)

  • 2016-09-20 Hermen

    Getting anxcious...

  • 2016-09-01 Hermen


  • 2016-08-23 weaverryan

    Haha, you're going to make it very easy to make sure we hit all the right topics for the tutorial :). Thanks for the continued input!

  • 2016-08-23 Hermen

    I had thought it would be something along those lines. Thanks for your feedback and also the tip about the bundle. I'll definitely look into it.

  • 2016-08-22 Victor Bocharsky

    Hey Hermen,

    For nice URLs, you simple should add a new *unique* string column in your DB table, i.e. "slug" or "alias" which will contain this pretty nice part of URL. And since it is unique, you could query entity by this slug instead of ID. And of course, in your route use "/news/{slug}" where you'll pass an entity slug to generate a valid route instead of "/news/{id}". The only problem - you should use only valid characters in your slugs and avoid such as "?", "#", etc. Thanks to the bundles, there're already a couple of third-party libs which help with generating a slug for Doctrine entities. Probably, the most popular is the "StofDoctrineExtensionsBundle" which has a lot of DoctrineExtensions's features includes a Sluggable one. So check it out.


  • 2016-08-20 Hermen

    And how to put all this information, from the Many, the One and the other Many, on the screen in a single page...

  • 2016-08-20 Hermen

    And, but you probably have this on the role already, when using a ManyToOne with separate joining entity (so, not a real ManyToMany, but a ManyToOne/OneToMany/ManyToOne), how to update that. You even might have ment that in your last sentence, but English is not my first language and neither is Symfony...

  • 2016-08-20 Hermen

    And while your at it, but this may be completely off (this) topic, nice URL's and getting stuff from the database. Example: When following the tutorials, a route is created, e.g. \news\watch-our-great-tutorial-about-manytomany. But, if I want to fetch that article from a database, I probably need to use something like \news\2345 where 2345 is the id of that news item. Or is there something out there that lets me fetch that article and show the nice URL?

  • 2016-08-16 weaverryan

    Yes, you bring up a valid point :). I rarely use ManyToMany because of this: I almost always use ManyToOne with an extra entity because it gives me the extra flexibility immediately. And yes, if you want fields like dateCreated, then you would need the extra entity even for these fields. That's why I don't often use ManyToMany. But, we'll still do a tutorial on it, and more about the really hard parts of OneToMany/ManyToOne - things like the owning/inverse side that you were asking about.


  • 2016-08-16 Hermen

    Ah, yes. I forgot, I read that somewhere. Maybe you could go into why not always use that solution even of there are no extra fields. By the way, the extra fields, does that include the 'record meta data' like "dateCreated" and "dateModified"?

  • 2016-08-15 weaverryan

    Thanks Hermen! The topic about updating from both sides is very important - this will probably be the most important part of the tutorial :). About the "extra data", I will also mention this, because it's really important. But, the answer is simple: if your join table needs even *one* extra field, then you should no longer use a ManyToMany relationship. Instead, you should build a new entity that will join the other two. For example, suppose you have a ManyToMany between Product and Category. If you need some extra field on the join table, then you need to create a new ProductCategory entity, which will have a ManyToOne to Product and a ManyToOne to Category. A true ManyToMany only works if there are *no* extra fields.

    And thanks again for the details!

  • 2016-08-12 Hermen

    How to go about it (how to set it up) one way and both ways (like OneToMany and ManyToOne), how to update a page with both sides ('main form' and 'sub form'), how to go about a many to many with extra data in the joining table. Like: I want to catalog my DVD's with movie title, synopsis, year released and rating (like Motion picture rating system which in my country holds more ratings, see so this is ManyToMany) and the main actors with their role in the movie (ManyToMany with extra data).

    Hope you can make anything out of this.

  • 2016-08-11 weaverryan

    Sorry about that - it obviously got pushed back beyond July. But, we'll be starting this tutorial very soon (like, next on my list). So, while I have you - is there anything specifically that's confusing? I'll be putting together the list of things to cover - want to make sure I hit everything important!


  • 2016-08-11 Maciek


    Any news according to "ManyToMany" relationship?

  • 2016-06-15 weaverryan

    Hey Hermen!

    I've added it to the schedule for July, but as an "extra" - we will do a few other casts in July before it. So, I can't promise July, but we'll do our best!


  • 2016-06-15 Hermen

    Anything on the schedule yet *wishfull thinking*?

  • 2016-06-10 Shairyar Baig

    yes thats what i ended up doing writing a custom query :)

  • 2016-06-08 weaverryan

    Hi Baig!

    Hmm, I'm not sure - I don't fully understand the data model. If there is a Request and I accept it, then I understand that this creates a ManyToMany between my User and the Request. But *before* I accept this Request, how is the Request linked to the User? I'm not sure about this part.

    But in general, when you have complex queries like this where ultimately you just need a "count" of something, I often write the query in SQL first (and test it in phpmyadmin or something similar). Then, I write that same query in DQL / the query builder.


  • 2016-06-07 Shairyar Baig

    Hi Ryan,

    I have a question. Suppose I have 2 entities `User` and `Request` and they are linked with each other in `manyToMany`

    Now the scenario is that I can also accept the request and you can also accept the same request and accepting the request creates the `manyToMany`

    So now lets suppose there are total of 3 requests out of which I have accepted 2 and you have accepted 3

    How on front end can we display the user the "Total number of requests" and "Total number of request accepted" and "total number of request that are pending to be accepted".

    Suppose in controller I have this

    $em = $this->getDoctrine()->getManager();

    //get all dinners to be listed as upcoming dinners in view
    $allRequests = $em->getRepository('AppBundle:Requests')->findAll();

    return $this->render('AppBundle::requests.html.twig', array(
    'allRequests' => $allRequests,

    And inside twig i have

    Here I would like to display the counts "Total Received", "Total Accepted" and "Total Pending"
    {% for requests in allRequests %}



    {% endfor %}

    How can this be achieved.


  • 2016-04-19 weaverryan

    It's not currently on the schedule, so it will likely *not* come out in the next 3-6 months. However, if we have more people ask for it, that might change :).

  • 2016-04-18 Maciek

    When should we expect "ManyToMany" course?

  • 2016-03-23 Hermen

    I'll be waiting for it!

  • 2016-03-20 weaverryan

    Ah, a power user - very nice find :D

  • 2016-03-20 weaverryan

    Hey Hermen! We decided to save ManyToMany for its own small tutorial - it's got some special aspects we can cover, like removing items and form collections. I want to get that all *just* right - because they can be a real joy to work with :)

  • 2016-03-20 Jonathan Keen

    You guys certainly know how to make addicts want more... ;) I can't tell you how often I come here to look for new tutorials to absorb. I'm even guilty of browsing to see when you sometimes publish the notes before the videos...which is what I was just doing.

  • 2016-03-20 Hermen

    Looking forward to these. Will you be dealing with ManyToMany also?