DEV Community

Hanane Kacemi
Hanane Kacemi

Posted on • Updated on

Symfony and Database

I'm having free time these days so I decided to go back to learn symfony. I picked up a course about doctrine and symfony. I will post my notes here, feel free to leave a comment, I am open to all suggestions.

Doctrine Installation :

Symfony 5 doesn’t come with a database layer when installing it, to be able to store data in a Database we need to add a library for that, like Doctrine. run : composer require orm

orm is an alias for a library called symfony/orm-pack

Pack is a shortcut to install several packages using one command, for example if we check the github repo of orm-pack, we see that it contains only a composer.json file where we have all dependencies(packages) needed for Doctrine.

Symfony and Docker :

Once the installation is done, you will notice that Symfony Flex added a docker-compose.yml file. Instead of using a local Database (ex :mysql) and attach it to our project let's take advantage of docker and the automatic configuration added by Flex.

docker-compose file

Check that Docker is installed on your machine, and then run :
docker compose up -d

To see the mysql Docker container, run :
docker compose ps

To connect to mysql :
docker compose exec database mysql -u root --password=password

Without Docker, you need to configure the environment variable DATABASE_URL in the .env file. Since we are using the local symfony server (by running symfony serve -d) we don’t need that, because when the symfony binary detects the docker-compose.yml file it reads all the config and environment variables of running services and make them available for our app.
You can run symfony var:export --multiline to see the value of environment variables.

To stop a container :
docker compose stop

That's all for today :)

Discussion (0)