DEV Community

Hanane Kacemi
Hanane Kacemi

Posted on

1

Symfony & Database-Part 2

In the previous post, we configured Symfony to use Docker for Database, in this post we will talk about Database creation(table, column...) using Doctrine and Symfony.

Create Database :

We can create a database with a simple command : symfony console doctrine:database:create

Doctrine is an ORM (Object Relational Mapper) : Each Table in Database (ex:employee) has a corresponding class in our App (known as Entity, ex : Employee.php) and each column of this table (ex:name) is related to a property of that class (ex: private $name;).

To create the entity class, run : symfony console make:entity

New file is created under src/Entity folder (I like to run after each command git status to check what has been generated by Symfony and access to the file to see its content).

The make:entity command can be used to create an new Entity or to update an existing one (add new property, a constraint...).

Now to create the table and its columns, we need to execute 2 commands, the 1st one is : symfony console make:migration

This command compares the actual state of the database with all Entity classes, if an Entity is newly created or modified the command creates the query for that changes under migrations folder in order to make database matches entities.
query generated for table creation

To execute this query, we run the 2nd command : symfony console doctrine:migrations:migrate

Symfony is smart enough :D to know if a query has already been executed : The migration system create a table in Database doctrine_migration_versions where it stores all migrations executed :

content of doctrine_migration_versions

Thanks for reading and Have a nice day ;)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay