Loading fixtures
Let us assume you have an existing project with a User
model.
To create a fixture for that model, there are three steps:
- create a fixture class.
- load that fixture with a loader.
- execute the fixture with an executor.
Creating a fixture class
Fixture classes have two requirements:
- They must implement
Doctrine\Common\DataFixtures\FixtureInterface
. - If they have a constructor, that constructor should be invokable without arguments.
1 <?php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;
class UserDataLoader implements FixtureInterface
{
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setUsername('jwage');
$user->setPassword('test');
$manager->persist($user);
$manager->flush();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Loading fixtures
To load a fixture, you can call Loader::addFixture()
:
It is also possible to load a fixture by providing its path:
If you have many fixtures, this can get old pretty fast, and you might want to load a whole directory of fixtures instead of making one call per fixture.
You can get the added fixtures using the getFixtures()
method:
Executing fixtures
To load the fixtures in your data store, you need to execute them. This is when you need to pick different classes depending on the type of store you are using. For example, if you are using ORM, you should do the following:
Each executor class provided by this package comes with a purger class that will be used to empty your database unless you explicitly disable it. |
If you want to append the fixtures instead of purging before loading
then pass append: true
to the execute()
method:
By default the ORMExecutor
will wrap the purge and the load of fixtures
in a single transaction, which is the recommended way, but in some cases (for
example if loading your fixtures is too slow and causes timeouts) you may
want to wrap the purge and the load of every fixture in its own transaction.
To do so, you can use MultipleTransactionORMExecutor
.