Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Installing Doctrine

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Welcome back team to episode three of our Symfony 6 series! The first two courses were super important: taking us from the basics up through the core of how everything works in Symfony: all that good "services" & configuration stuff. You are now ready to use any other part of Symfony and really start building out a site.

And... what better way to do that than to add a database? Because... so far, for all the cool things we've done, the site we've been building is 100% static. Boring! Time to change that.

Hello Doctrine

So we know that Symfony is a collection of a lot of libraries for solving a ton of different problems. So... does Symfony have some tools to help us talk to the database? The answer is... no! Because... it doesn't have to!

Why? Enter Doctrine: the most powerful library in the PHP world for working with databases. And Symfony and Doctrine work great together: they're the Frodo and Sam Gamgee of PHP middle earth: the Han Solo and Chewbacca of the PHP Rebel Alliance. Symfony & Doctrine are like two Disney characters that finish each other's sandwiches!

Project Setup

To see this dynamic duo in action, let's get our project set up. Playing with databases is fun, so code along with me! Do that by downloading the course code from this page. After unzipping it, you'll find a start/ directory with the same code that you see here. Pop open this README.MD file for all the setup instructions.

The last step will be to open a terminal, move into your project and run:

symfony serve -d

This uses the Symfony binary to start a local web server which lives at https://127.0.0.1:8000. I'll take the lazy way out and click that to see... Mixed Vinyl! Our latest startup idea - and I swear, this one is going to be huge - combines the nostalgia for the "mix tapes" of the 80's and 90's with the audio experience of vinyl records. You craft your sweet mix tapes, then we press them onto a vinyl record for a full hipster audio experience.

So far, our site has a homepage and a page to browse mixes that other people created. Though, that page isn't really dynamic: it pulls from a GitHub repository... and unless you've configured an API key like we did in the last episode, this page is broken! That's the first thing we'll fix: by querying a databasse for the mixes.

Installing Doctrine

So let's get Doctrine installed! Find your terminal and run:

composer require "doctrine:^2.2"

This is, of course, a Flex alias for a library called symfony/orm-pack. And remember: a "pack" is a, sort of, "fake library" that serves as a shortcut to install several packages at once. In this case, we're installing Doctrine itself, but also a few other relataed libraries, like the excellent Doctrine Migrations system.

Docker Configuration

Oh, and check this out! The command is asking:

Do you want to include Docker configuration from recipes?

So, occasionally when you install a package, that package's recipe will contain Docker configuration that can, for example, start a database container. This is totally optional, but I'm going to say p for yes permanently. We'll talk more about the Docker configuration in a few minutes.

The Doctrine Recipes

But right now, let's check out what the recipe did. Run:

git status

Okay cool: this modified the normal files like composer.json, composer.lock and symfony.lock... and it also modified config/bundles.php. If you check that out... no surprise: our app now has two new bundles: DoctrineBundle and DoctrineMigrationsBundle.

... lines 1 - 2
return [
... lines 4 - 13
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
];

But probably the most important part of the recipe is the change it made to our .env file. Remember: this is where we can configure environment variables... and the recipe gave us a new one called DATABASE_URL. This, as you can see, holds all the connection details, like the username and password.

30 lines .env
... lines 1 - 27
DATABASE_URL="postgresql://symfony:ChangeMe@127.0.0.1:5432/app?serverVersion=13&charset=utf8"
... lines 29 - 30

What uses this environment variable? Excellent question! Check out a new file the recipe gave us: config/packages/doctrine.yaml. Most of this config you won't need to think about or change. But notice this url key: it reads that DATABASE_URL environment variable!

doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
... lines 4 - 43

The point is: the DATABASE_URL env var is the key to setting up your app to talk to a database... and we'll play with it in a few minutes.

The recipe also added a few new directories: migrations/ src/Entity/ and src/Repository/. Right now, other than a meaningless .gitignore file, these are all empty. We'll start filling them up real soon.

Ok: Doctrine is now installed. But to talk to a database... we need to make sure we have a database running and that the DATABASE_URL environment variable is pointing to it. Let's do that next, but with an optional & delightful twist: we're going to use Docker to start the database.

Leave a comment!

4
Login or Register to join the conversation
Joshua C. Avatar
Joshua C. Avatar Joshua C. | posted 1 month ago

Hey! The audio stops at 5:22 and the video froze too!

Reply

Hey Craig,

We're sorry to hear you're experiencing problems with our video player! I just double-check it, the video plays well for me around those 5:22, so it might be an internet connection issue on your laptop and so the video was not able to load from the server. Or, it might be a temporary outage of Vimeo video service - our video hosting provider. Please, try again today and let us know if the problem still persist. In this case, as a temporary workaround, I'd recommend you to download the video and watch it locally.

I hope this helps.

Cheers!

Reply
Joshua C. Avatar

The video player still doesn't work for me in the website. The downloaded video works great. I don't have any trouble with any other videos on the site (I've watched dozens), so I assumed it was an issue with this video specifically.

Reply

Hey Craig,

Glad to hear you were able to watch the downloaded video in full locally. I'd recommend you to try a different browser, or probably a Chrome Incognito mode might help as well. But since you already watched it locally, you may just ignore :)

I still can't reproduce it for myself, so I think it should be good and most probably it's something local.

Cheers!

Reply
Cat in space

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

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^3.7", // v3.7.0
        "doctrine/doctrine-bundle": "^2.7", // 2.7.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
        "doctrine/orm": "^2.12", // 2.12.3
        "knplabs/knp-time-bundle": "^1.18", // v1.19.0
        "pagerfanta/doctrine-orm-adapter": "^3.6", // v3.6.1
        "pagerfanta/twig": "^3.6", // v3.6.1
        "sensio/framework-extra-bundle": "^6.2", // v6.2.6
        "stof/doctrine-extensions-bundle": "^1.7", // v1.7.0
        "symfony/asset": "6.1.*", // v6.1.0
        "symfony/console": "6.1.*", // v6.1.2
        "symfony/dotenv": "6.1.*", // v6.1.0
        "symfony/flex": "^2", // v2.2.2
        "symfony/framework-bundle": "6.1.*", // v6.1.2
        "symfony/http-client": "6.1.*", // v6.1.2
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/proxy-manager-bridge": "6.1.*", // v6.1.0
        "symfony/runtime": "6.1.*", // v6.1.1
        "symfony/twig-bundle": "6.1.*", // v6.1.1
        "symfony/ux-turbo": "^2.0", // v2.3.0
        "symfony/webpack-encore-bundle": "^1.13", // v1.15.1
        "symfony/yaml": "6.1.*", // v6.1.2
        "twig/extra-bundle": "^2.12|^3.0", // v3.4.0
        "twig/twig": "^2.12|^3.0" // v3.4.1
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
        "symfony/debug-bundle": "6.1.*", // v6.1.0
        "symfony/maker-bundle": "^1.41", // v1.44.0
        "symfony/stopwatch": "6.1.*", // v6.1.0
        "symfony/web-profiler-bundle": "6.1.*", // v6.1.2
        "zenstruck/foundry": "^1.21" // v1.21.0
    }
}