Get started with Symfony 6 for beginners — Part 3| Database, Doctrine, Entity.

Azay Karimli
3 min readMay 21, 2022

In this article, we will create an entity and then migrate it to the database to which we are connected. Symfony uses Doctrine to talk with the database. Thanks to Doctrine, it provides a variety of tools needed to use databases in your applications. These tools support relational databases like MySQL and PostgreSQL and NoSQL databases like MongoDB. Doctrine is the set of libraries that works with databases.

1. Creating an Entity Class.

In a traditional OOP way when we need to show anything from the database, we automatically think about objects to show or do other procedures on them. With the Entity, these classes and required fields are automatically created and make life easier. I mean much easier. Seriously it saves a lot of time. You can start planning your world tour finally because of all the time you have now. What I am trying to say is that it saves a lot of time.

So, let’s make one. For that as always go to the directory where your Symfony project is located and type the line below.

$ symfony console make:entity

This command will result in asking you several questions.

First name of the entity you would like to create. I will use “blog” as an example to create an application. This will create 2 new files: “src/Entity/Blog.php” and “src/Repository/BlogRepository.php”.

Then it will follow by asking the name of the properties which will be something like follows:

title, type string, length 40, nullable - no
short_description, type string, length 40, nullable - no
body, type text, nullable - no

Press ctrl + c to escape.

2. Customize the .env file.

The database connection information is stored as an environment variable called DATABASE_URL. For development, you can find and customize this inside .env:

# In all environments, the following files are loaded if…