Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

The "symfony console" Command & server_version

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.

Doctrine is now configured to talk to our database, which lives inside a Docker container. That's thanks to the fact that the Symfony dev server exposes this DATABASE_URL environment variable, which points to the container. For me, the container is accessible on port 50739.

Now let's make sure the actual database has been created. But first, in index.php, remove the dd()... then close that file.

Spin over to your terminal and run:

php bin/console

This prints every bin/console command that's available including a bunch of new ones that start with the word doctrine. Ooh. Most of these aren't very important and we'll walk through the ones that are along the way.

bin/console doctrine:database:create

For example, one is called doctrine:database:create. Cool, let's try it:

php bin/console doctrine:database:create

And... error! Look closely: it's trying to connect to port 5432. But our environment variable is pointing to port 50739! It's as if it's using the DATABASE_URL value from our .env file instead of the real one that's set by the Symfony binary.

And, in fact, that's exactly what's happening. And, it makes sense! When we refresh the page in our browser, that's processed through the symfony binary, which gives it the opportunity to add the environment variable.

But when we run a bin/console command - where console is just a PHP file that lives in a bin/ directory, the symfony binary is never used as part of that process. This means it never has the opportunity to add the environment variable. And so, Symfony falls back to using the value from .env.

To fix this, whenever we run a bin/console command that needs the Docker environment variables, instead of running bin/console, run symfony console:

symfony console doctrine:database:create

That's literally a shortcut to running bin/console: it's no different. But the fact that we're executing it through the symfony binary gives it the opportunity to add the environment variables.

When we try this... yes! We do get an error because apparently the database already exists, but it did successfully connect and talk to the database.

Configuring the server_version

Ok, there's one last bit of configuration that we need to set. Open config/packages/doctrine.yaml. This file came from the recipe. Find server_version and un-comment it.

doctrine:
dbal:
... lines 3 - 6
server_version: '13'
... lines 8 - 43

This value "13" is referring to the version of my database engine. Since I'm using Postgres version 13, I need 13 here. If you're using MySQL, you might need 8 or 5.7.

This helps Doctrine determine which features your database does or doesn't support... since a newer version of a database might support features that an older version doesn't. It's not a particularly interesting piece of configuration, we just need to make sure it's set.

Ok team: all the boring setup is done. Next: let's create our first entity class! Entities are the most foundational concept in Doctrine and the key to talking to our first database table.

Leave a comment!

8
Login or Register to join the conversation
Carlos-D Avatar
Carlos-D Avatar Carlos-D | posted 1 month ago

I'm trying to connect to the database with symfony console doctrine:database:create but the error message I receive is 'SQLSTATE[08006] [7] connection to server at "127.0.0.1", port 5432 failed: Connection refused. Is the server running on that host and accepting TCP/IP connections?'

Thank you, ;)

Reply

HI,

Are you using docker? if so are you running it from PHPStorm console?

Cheers!

Reply
Carlos-D Avatar

I'm using Docker and I've tried with Visual Code / PHPStorm and macOs native terminal / iTerm2 and Visual code / PHPStorm terminals and I get the same error.

If I try another postgresql database manager like DBngin (for example) I've no problem configuring the database.

I'm sure I'm missing some step but I don't know what, ;););););)

Reply

Hey Carlos-D!

Hmm, let's see :). Ok, so the error is this:

SQLSTATE[08006] [7] connection to server at "127.0.0.1", port 5432 failed: Connection refused

That tells me that Symfony is reading the DATABASE_URL from your .env file, as this is the default port used in that file. However, if you're using the Docker setup we've provided, that value should be overridden by a real DATABASE_URL env var. The process looks like this:

A) You run docker-compose up -d to start the Docker contain that ships with the code (did you do this step? It seems like the most likely candidate for the problem).

B) Once you do this, whenever you run symfony console ..., the symfony binary will detect that you have a Docker container called database running for postgres. It will then read the connection parameters to that Docker container and expose a real environment variable called DATABASE_URL that points to it.

C) This means that, when the command actually runs, it will talk to the Postgres instance in your Docker container.

Another way to see if the DATABASE_URL environment variable is being properly added by the symfony binary is to run symfony var:export --multiline to see if you see it in the list. It should look something like DATABASE_URL=postgres://symfony:ChangeMe@127.0.0.1:56986/app?sslmode=disable&charset=utf8.

Cheers!

Reply
Fouad B. Avatar
Fouad B. Avatar Fouad B. | posted 3 months ago

the video stops playing due to an error happening at the 2:07 minute mark. Chrome throws an error: "The media playback was aborted due to a corruption problem or because the media used features your browser did not support.", safari will just freeze and seemingly play but both audio and video stop.

Reply

Hey Found,

We're sad to hear you're experiencing some problems with playing the video in Safari! What version of the browser do you use? Can you try to upgrade it to the latest? Mine is "Version 15.5 (17613.2.7.1.8)" and I cannot reproduce it, it works great for me. Please, could you try in a different browser, like Google Chrome? Is it the same error and at the same second for you?

Also, here's a little tip: as a temporary workaround you can download the video and watch it locally. I understand it's not perfect but could help in this specific case.

Cheers!

Reply
Fouad B. Avatar

It is on MacOS Monteray 12.5
Safari Version 15.6 (17613.3.9.1.5)

And regarding Chrome, it happens in chrome with the stated error message.
Chrome Version 104.0.5112.79 (Official Build) (x86_64).

It only happens in this video on the mentioned minute mark.

Thanks for the tip ;) will do that.

Cheeers.

Reply

Hey Found,

Hm, that's weird that it happens only on the specific video and only at the specific time, and out team still can't reproduce it. It makes me thinks that it might be a temporary Vimeo issue because all our videos are hosted on Vimeo. I hope it will be better soon, but nothing much we can do here. I'd recommend you to download problematic videos and watch them locally.

Sorry for the inconveniences!

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
    }
}