3

As much as I know, a fixture is "something used to consistently test some item, device, or piece of software". It should be used only in dev and test environnement.

My context : I have a web app which proposes to sell 5 specific products. There is not a backoffice to edit/remove/add a product, but there is a backoffice to modify product prices and description, that is why I need the 5 products in a database. To initialize the app, I would want to insert my 5 products in the database (by the code).

I could use fixtures to insert products in my database, but I think it's not the job of the fixture which should only be used in "dev" or "test" env.

"doctrine:schema:update" doesn't seem to be the right solution either, because it only updates the schema (the structure of my database), it's not about data.

Is there a way to insert "static data" in my database with Symfony3 ? A system of data migration ? Is there a right way to do ? Using the doctrine-migrations-bundle ?

Dam Fa
  • 448
  • 4
  • 16

2 Answers2

3

Well, you have to put some on fixture because you will still need that data in dev / test environnement.

For initialisation, you have to either put it in the first migration script (As I understand it, it is required for your application to work)

Or you abandon the idea of scripting a sql that would be executed once in your project lifetime and you create a initial-prod-data.dump.sql than you import when you initialize your project in prod.

PS: if you use migration, don't use entity, use plain sql to avoid any regression involving model evolution.

goto
  • 7,908
  • 10
  • 48
  • 58
1

Fixtures is the way to insert static data for testing. But yes it should be used in dev env because it is totally truncates all data in db before inserting. Migrations is for schema version control. So it is not the right place to use it. So stick with fixtures if you are ok with it.

Artur Yukhatov
  • 190
  • 1
  • 9