Search code examples
symfonydoctrine-ormdoctrinedatabase-migrationsymfony5

Doctrine - how to initialize DB values


I use Doctrine 2.7.0 in a Symfony 5.0 Application. I define my DB tables within my Entities with ORM Annotations. I create a migration with Symfony maker like this

php bin/console make:migration

then I create the raw DB (mySql) from that with

php bin/console doctrine:migrations:migrate

This results in a nice fresh DB.

Now - I am wondering if it is possible to define base data for the tables with Annotations. I don't mean to set default values for properties but to add entries to tables which the DB allways needs.

So basically I want the migration to insert entries to the DB too. Is that possible?


Solution

  • Data that is required for the application to run like lookup tables should actually be part of the migration. You should also provide the proper rollback procedure for that data in order to keep your migration backwards compatible.

    You should however refrain from using Doctrine for those migrations, since a later change to the Entity mapping etc. would break all previous migrations.

    You could technically use fixtures to load this data. Though by default fixtures truncate the entire database unless the --append flag is specified leaving it rather unsuited for this kind of operation as removing data sets with this option is not possible.