You are browsing a version that is no longer maintained. |
Fixture ordering
There are two interfaces you can implement in your fixtures to control in which order they are going to be loaded.
- By implementing
OrderedFixtureInterface
, you will be able to manually specify a priority for each fixture. - By implementing
DependencyFixtureInterface
, you will be able to declare which class must be loaded after which classes (note the plural), and let the package figure out the order for you.
You may implement an interface in a fixture, and another interface
in another fixture, and even no interface (besides
|
Option 1: Controlling the order manually
1 <?php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
final class MyFixture extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager): void
{
// …
}
public function getOrder(): int
{
return 10; // smaller means sooner
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
While extending |
Option 2: Declaring dependencies
If you have many models, and a project that evolves, there may be
several correct orders. Using OrderedFixtureInterface
may become
impractical in case you need to insert a new fixture in a position where
there is no gap in the order. Instead of always renumbering the
fixtures, or being careful to leave big gaps, you can declare that your
fixture must be loaded after some other fixtures, and let the package
figure out what to do.
1 <?php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
class MyFixture extends AbstractFixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
}
/**
* @return list<class-string<FixtureInterface>>
*/
public function getDependencies(): array
{
return [MyOtherFixture::class];
}
}
class MyOtherFixture extends AbstractFixture
{
public function load(ObjectManager $manager): void
{}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27