13 March 2020

Symfony 4: Grouping Data Fixtures

Ever wanted to load fixtures without touching existing data? Symfony's DoctrineFixturesBundle has a grouping feature that can do exactly that.

My use case is slightly different as i needed something that i can run separately from the basic fixtures like initial normal/admin users, pre defined site settings, etc. I needed to migrate users from an existing site and will have to re-load the fixtures multiple times to deal with data issues as some migrated data are not ideal and may even cause errors.

Below are the steps I had to do:

1. First thing to do is separating the fixtures in different file classes. (see doc)

2. The problem with step 1 is by default, even if the fixture classes are in separate files, they will all be executed. So i need to group them and execute loading of fixtures by specifying the specific group i want to load/run at any time. (see doc)

ex:
$ php bin/console doctrine:fixtures:load --group=group-basic
$ php bin/console doctrine:fixtures:load --group=group-migrate --append

3. Then if errors keep on showing up specially on the migration of users, i can then just keep running that specific fixture, fixed data issues then run it again.

$ php bin/console doctrine:fixtures:load --group=group-migrate --append