Symfony News

New in FixturesBundle: Group your Fixtures

Ryan Weaver

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 7.1 or earlier, as you can read in the DoctrineFixturesBundle 3.1.0 changelog.


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


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Find us on Twitter

Find us on Facebook

ADS