Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a bundle can provide "default data" i.e. pre-filled tables in Symfony 2?

I think I've a good understanding of Symfony and how bundle works.

However I've never found how to solve a simple problem: make a reusable bundle that provides data like tables/Doctrine entities pre-filled with (i.e.) all country names in the world, all provinces of Italy, tax rates history in England and so on.

Of course the purpose is to provide forms, services and controllers relying on this data source, without the need to copy and paste tables and entities across projects.

How would you do that?

Data fixtures IMHO are not an option because an obvious reason: you are going to purge your database while it's running.

A custom command reading from a static data-source (json, YAML) and performing inserts/updates?

like image 598
gremo Avatar asked Sep 06 '14 21:09

gremo


1 Answers

First step is declaring a Doctrine entity in your Bundle. I think you should create DataFixtures to populate your datas into db.

You maybe should consider to use Seeds instead of Fixtures.

Fixtures are fake datas, used to test your application

Seeds are the minimal datas required for your application to work.

Technically, these are exactly the same thing, you declare it under the "DataFixtures/" folder and you import them with the "doctrine:fixtures:load" command.

You can create a folder "Fixtures/", and a folder "Seeds/" under the folder "DataFixtures", then load your seeds with the command

php app/console doctrine:fixtures:load --fixtures=/path/to/seeds/folder --append

It was suggested in the comments that it may be safer, especially in production environment, to create a custom Symfony2 command to force the "--append" mode. Without this mode, your database will be purged, and you could loose your production data.

like image 170
Paul Andrieux Avatar answered Sep 21 '22 18:09

Paul Andrieux