New in FixturesBundle: Group your Fixtures
December 21, 2018 • Published by Javier Eguiluz
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.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.