> Symfony 5 >

Course Overview

Login to bookmark this course

Mastering Doctrine Relations

Discover the magic of Doctrine Relations in Symfony: Learn to create ManyToOne and ManyToMany relations and querying with Joins.

  • 2945 students
  • EN Captions
  • EN Script
  • Certificate of Completion

Your Guides

About this course

This tutorial also works great for Symfony 6!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^3.3", // v3.3.0
        "composer/package-versions-deprecated": "^1.11", // 1.11.99.3
        "doctrine/doctrine-bundle": "^2.1", // 2.4.2
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
        "doctrine/orm": "^2.7", // 2.9.5
        "knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
        "knplabs/knp-time-bundle": "^1.11", // v1.16.1
        "pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
        "pagerfanta/twig": "^3.3", // v3.3.0
        "sensio/framework-extra-bundle": "^6.0", // v6.2.1
        "stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
        "symfony/asset": "5.3.*", // v5.3.4
        "symfony/console": "5.3.*", // v5.3.7
        "symfony/dotenv": "5.3.*", // v5.3.7
        "symfony/flex": "^1.3.1", // v1.21.6
        "symfony/framework-bundle": "5.3.*", // v5.3.7
        "symfony/monolog-bundle": "^3.0", // v3.7.0
        "symfony/runtime": "5.3.*", // v5.3.4
        "symfony/stopwatch": "5.3.*", // v5.3.4
        "symfony/twig-bundle": "5.3.*", // v5.3.4
        "symfony/validator": "5.3.*", // v5.3.14
        "symfony/webpack-encore-bundle": "^1.7", // v1.12.0
        "symfony/yaml": "5.3.*", // v5.3.6
        "twig/extra-bundle": "^2.12|^3.0", // v3.3.1
        "twig/string-extra": "^3.3", // v3.3.1
        "twig/twig": "^2.12|^3.0" // v3.3.2
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
        "symfony/debug-bundle": "5.3.*", // v5.3.4
        "symfony/maker-bundle": "^1.15", // v1.33.0
        "symfony/var-dumper": "5.3.*", // v5.3.7
        "symfony/web-profiler-bundle": "5.3.*", // v5.3.5
        "zenstruck/foundry": "^1.1" // v1.13.1
    }
}

In the part 1 of the Doctrine Tutorial we got some serious work done with Doctrine: creating entity classes, making custom queries, migrations, Docker setup & more!

But... we ignored one of the biggest part of Doctrine: relations / associations! And thanks to tooling inside of Symfony, creating database relationships - ManyToOne, ManyToMany, etc - has never been easier. So let's go!

  • Generating a ManyToOne relationship (association)
  • Relation annotations / PHP 8 attributes
  • The OneToMany inverse side of a relation
  • Handling relationships in fixtures with Foundry
  • The owning versus inverse sides of a relation
  • Doing magic with the ArrayCollection (Criteria)
  • Querying with Joins
  • ManyToMany Relations

So let's cook up some relationships and take our Cauldron Overflow app to the next level!

Next courses in the Symfony 5: The Fundamentals section of the Symfony 5 Track!

16 Comments

Sort By
Login or Register to join the conversation
Ilya E. avatar Ilya E. 3 years ago

Hello, when is the release date? Can't wait... =)

| Reply |

Hey Ilya,

Thank you for your interest in SymfonyCasts tutorials! This course should be the next after "Symfony UX: Turbo" that is releasing right now. I think this should happen in about 2 weeks.

Thank you for your patience!

Cheers!

2 | Reply |

Is this almost ready for launch?

| Reply |

Yup! I'm building it this week - Turbo had a few more chapters than I expected. I'm hoping to release this next week :)

| Reply |

Looking forward to it! These doctrine courses are the reason I got a subscription. I'm currently working on a massive warehouse management database and my implementation of foundry for testing is incredibly hacky

1 | Reply |

Hey Nick F.!

> Looking forward to it! These doctrine courses are the reason I got a subscription

Sweet!

> my implementation of foundry for testing is incredibly hacky

We won't be talking about Foundry and testing in this tutorial. But if you have any specific questions, I'd be happy to do my best to answer them here :).

Cheers!

| Reply |

Is there a mistake in the course description then? One of the topics is "Handling relationships in fixtures with Foundry"

| Reply |

Oh, yes! We will cover that - just not using Foundry specifically inside of tests :).

| Reply |

Ok, it'll release NEXT week actually - but right away on Monday, then we'll move through quickly. Thanks for your patience on this one - I had some "things" come up personally that were straining my work time!

| Reply |
Lionel-F avatar Lionel-F 3 years ago

Hi, Would you deal with connections to multiple databases and make:migration/doctrine:migration:migrate to multiple databases (I don't know if this one is possible ...) ?

| Reply |

Hey Lionel,

Unfortunately, Maker bundle only generates migrations for MySQL database. But in case you have configured multiple connections to multiple DB in your project, you can use DoctrineMigrationBundle's commands, e.g. "bin/console doctrine:migrations:diff --conn=CONN" Where CONN is your connection. Or just add --help option to any Symfony command to see more options that you can use. I believe the --conn option is exactly what you need in your case.

I hope this helps!

Cheers!

1 | Reply |

Hello victor , Doctrine Migrations also generate migrations for PostgreSQL.

| Reply |

Hey Abdelkarim,

Yes, DoctrineMigrationsBundle does, but Maker does not IIRC :)

Cheers!

| Reply |

maker works also with PosgreSQL ;)

| Reply |

Hey Abdelkarim,

Ah, really? I didn't know :) IIRC it didn't at some point in time. Thanks for confirming it works, then it's an easy win with Maker as well

Cheers!

| Reply |

Ty I will ty this :)

| Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!