1 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
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.
.. note::
You may implement an interface in a fixture, and another interface
in another fixture, and even no interface (besides
``FixtureInterface``) in a third one. Implementing both in the same
fixture is an error.
Option 1: Controlling the order manually
----------------------------------------
.. code-block:: php
<?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
}
}
.. note::
While extending ``AbstractFixture`` is not required, it is likely
you are going to need it since people usually need fixtures to be
loading in a specific order because of references from one fixture
to the other.
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.
.. code-block:: php
<?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
{}
}
|