Sharing objects between fixtures

Your models are likely to have relationships with each other. Because of that, it can be interesting to create and persist an object in a fixture, and then reference it in another fixture.

Assuming you have a User and a Role model, here is an example showing how to use the AbstractFixture class to do that.

AbstractFixture implements FixtureInterface, which means the requirement mentioned in the previous how-to guide is satisfied if you extend AbstractFixture.

1<?php namespace MyDataFixtures; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Persistence\ObjectManager; class UserRoleDataLoader extends AbstractFixture { public function load(ObjectManager $manager): void { $adminRole = new Role(); $adminRole->setName('admin'); $anonymousRole = new Role(); $anonymousRole->setName('anonymous'); $manager->persist($adminRole); $manager->persist($anonymousRole); $manager->flush(); // store reference to admin role for User relation to Role $this->addReference('admin-role', $adminRole); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

And the User data loading fixture:

1<?php namespace MyDataFixtures; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Persistence\ObjectManager; class UserDataLoader extends AbstractFixture { public function load(ObjectManager $manager): void { $user = new User(); $user->setUsername('jwage'); $user->setPassword('test'); $user->setRole( $this->getReference('admin-role') // load the stored reference ); $manager->persist($user); $manager->flush(); // store reference of admin-user for other Fixtures $this->addReference('admin-user', $user); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Note that because that last fixture depends on the first one, the order in which fixtures are loading becomes important. You can learn more about how to manage loading order in the dedicated guide.