Working with Doctrine Symfony Part 1 - Installing bundles and understanding main concepts

In this post we will be looking at Doctrine, how to install it and going through some main concepts

In order to work with the database in Symfony, we will be using Doctrine, an open source library which is focused on database storage and object mapping.

By default, if you install Symfony from composer, you will get all the dependencies for working with doctrine, but in case you don't have them, you can install them by running:

composer require symfony/orm-pack
composer require --dev symfony/maker-bundle

Configuration for doctrine package is located in config/packages/doctrine.yaml file. You can leave that as is, as the defaults are fine and you can work with those without a problem. If you want to check what those properties mean, please refer here

In order to configure our application and use database, we will need to edit the .env file. In the .env file, you will find connection string:


Everything here is self explanatory, you should change user, password and database. We still did not create a new database, so, in the next step, we will create a new database with the name you specified in the connection string.

Next, we will create our database from the terminal. Open your terminal and type:

php bin/console doctrine:database:create

or if you don't have php as global variable

bin/console doctrine:database:create

This will connect to your database server, authenticate with username and password you specified and create a new database with the database name you specified.

There are 2 ways of syncing your database with entities you create in your application:

First way is to use built in commands from doctrine bundle. This is fast way of deploying any changes, but should be used only in development. Also, it doesn't give you too much flexibility. In order to execute this, just open your terminal and type:

bin/console  doctrine:schema:update --force

For this tutorial, we will use a more flexible approach and one that should be used in production environment. First, we need to install the DoctrineMigrationsBundle, run:

composer require doctrine/doctrine-migrations-bundle "^1.0"

This will install the necessary bundle that will allow us to run migrations. The migrations will be stored in src/Migrations .There are 3 commands that you need to know:

bin/console doctrine:migrations:generate

This will generate an empty migration class. Generating empty migrations is useful when you want to add seed data, data that should be applied to all instances of the databases over multiple environments. Example of this data is if you have a table which defines types that will be constant and should have the same Ids over all instances.

bin/console doctrine:migrations:diff

This will generate a new migration, but will add only differences between your current code and the database structure. This is done in order to update your database with the new structure that you create in Entities PHP Classes.

bin/console doctrine:migrations:migrate

This will execute all migrations that have not been executed. DoctrineMigrationsBundle creates a table in your database where it will keep track of all the migrations that were executed. So, when you execute this command, it will take that table into account and will execute migrations that are not there, but present as PHP Classes in src/Migrations folder.

In the second part we will be creating our first entity.

Comments (0)

Leave a comment

Dire Solutions Logo Dark Vertical