This content originally appeared on DEV Community and was authored by hanane kacemi
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.
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 :
Thanks for reading and Have a nice day ;)
This content originally appeared on DEV Community and was authored by hanane kacemi
hanane kacemi | Sciencx (2021-12-11T10:11:19+00:00) Symfony & Database-Part 2. Retrieved from https://www.scien.cx/2021/12/11/symfony-database-part-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.