New in FixturesBundle: Group your Fixtures

Written by Symfony blog - - Aggregated on Friday December 21, 2018

Contributed by
Ryan Weaver
in #260.

DoctrineFixturesBundle, the bundle that integrates Doctrine's data fixtures library in Symfony applications, has just released its 3.1.0 version.

The most important new feature of this version is the possibility of organizing your fixtures in groups to load some but not all of them. This is configured using the getGroups() method defined in the new FixtureGroupInterface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// src/DataFixtures/UserFixtures.php
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;

class UserFixtures extends Fixture implements FixtureGroupInterface
{
    // ...

    public static function getGroups(): array
    {
        return ['group1', 'group2'];
    }
}

Once you have defined the group or groups each fixture class belongs to, pass the new --group option to load only the fixtures associated with those groups:

1
2
3
4
5
# only load the 'group1' fixture classes
$ ./bin/console doctrine:fixtures:load --group=group1

# the '--group' option is multiple, so you can load several groups
$ ./bin/console doctrine:fixtures:load --group=group1 --group=group2

In order to improve your productivity, each fixture class is added to a group whose name matches the short name of the class. In the previous example, the class also belongs to a group named UserFixtures. This allows to load just one fixtures class:

1
$ php bin/console doctrine:fixtures:load --group=UserFixtures

The new bundle version also includes other minor improvements and drops support for PHP versions lower than 7.1, as you can read in the DoctrineFixturesBundle 3.1.0 changelog.

Be trained by Symfony experts - 2019-01-14 Clichy - 2019-01-14 Clichy - 2019-01-16 Clichy

« The challenge for 2019 has just got real - DragonBe's PHP blog

Derick Rethans - The Confused C-Driver »