10

I am new to learning Symfony, (Symfony 4.1.6) and can't figure this out.

I have a database, and it contains a lot of tables for populating html select type inputs. they are in the database to ensure Foreign Key constraints in other records.

Is there a way to "add" this data to the database and track it? (via git) or should I resort to keeping an SQL file of just the "inserts"

I have looked at Fixtures but they seem to be mostly for dummy data. I want to insert real data. What is the "best practice" if any to do this?

****UPDATE****
I should add that the development of this project started outside Symfony, and so the database had already been manually created with a handful of records. I used bin/console doctrine:mapping:import 'App\Entity' annotation --path=src/Entity to build the entities.

Is there a similar way to "suck" the few records out of the db as well?

Maybe I just need to manually add the SQL insert statements to the first (or second) migrate file.

Chad
  • 1,139
  • 17
  • 41
  • 1
    Upon further reading and inspection of fixtures, I see they are setup as "dev" environment only - and truly not for production use. I am leaning towards migrations as this seems to make to the most sense. I am hoping there might be an "automagic" way to generate this though. – Chad Nov 01 '18 at 13:37
  • Migrations make diff between your entities and current db layout, and auto generate migration class upon this. – Łukasz Jakubek Nov 02 '18 at 06:26

3 Answers3

14

In symfony, working with production database, generaly based on creating and executing migrations thanks to DoctrineMigrationBundle.

Always when you make some changes in entity, you need to update database layout. You do this by creating migration by command doctrine:migrations:diff, review migration class carefully and after that execute it on production server with command doctrine:migrations:migrate. This is about database schema.

In turn, when you need to insert, delete or update dome predefined data in database, you could create empty migration class by doctrine:migrations:generate and fill with whatever you want. Some examples are: - Write raw sql queries and execute - this is the easiest way, good for simple data - Create entities and persist by entity manager - Load database fixtures - this is the most complex solution.

In this way you do not lost data already stored in production database when you will upgrade your app.

More details in DoctrineMigrationsBundle docs: https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html and also DoctrineFixturesBundle docs: https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

Łukasz Jakubek
  • 995
  • 5
  • 12
1

Fixtures are good for this use-case as well, but be aware not to accidentally overwrite anything.

If you only need to populate these once, migrations are a good option. (You should be using that anyway to manage schema changes.) For example, I almost always use a migration to insert admin users and some other really "fixed" data.

Padam87
  • 1,013
  • 1
  • 6
  • 7
  • how do you get migrations to "capture" data, not just schema changes? This sounds more appropriate to me. – Chad Oct 31 '18 at 19:16
  • Just write SQL like you normally would. Migrations will help you with tracking what is installed, and what is not. – Padam87 Oct 31 '18 at 20:48
  • 1
    You could also consider ContainerAware migrations https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html#container-aware-migrations – Padam87 Oct 31 '18 at 20:49
1

maybe you're looking for https://phinx.org/ or something like that ?

edit : http://docs.phinx.org/en/latest/migrations.html#inserting-data