Newer
Older
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Doctrine Data Fixtures Extension
[](https://travis-ci.org/doctrine/data-fixtures)
This extension aims to provide a simple way to manage and execute the loading of data fixtures
for the [Doctrine ORM or ODM](http://www.doctrine-project.org/). You can write fixture classes
by implementing the [`Doctrine\Common\DataFixtures\FixtureInterface`](lib/Doctrine/Common/DataFixtures/FixtureInterface.php) interface:
```php
namespace MyDataFixtures;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
class UserFixtureLoader implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$user = new User();
$user->setUsername('jwage');
$user->setPassword('test');
$manager->persist($user);
$manager->flush();
}
}
```
Now you can begin adding the fixtures to a loader instance:
```php
use Doctrine\Common\DataFixtures\Loader;
use MyDataFixtures\UserDataLoader;
$loader = new Loader();
$loader->addFixture(new UserDataLoader());
```
You can load a set of fixtures from a directory as well:
```php
$loader->loadFromDirectory('/path/to/MyDataFixtures');
```
Or you can load a set of fixtures from a file:
```php
$loader->loadFromFile('/path/to/MyDataFixtures/MyFixture1.php');
```
You can get the added fixtures using the getFixtures() method:
```php
$fixtures = $loader->getFixtures();
```
Now you can easily execute the fixtures:
```php
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
$executor->execute($loader->getFixtures());
```
If you want to append the fixtures instead of purging before loading then pass true
to the 2nd argument of execute:
```php
$executor->execute($loader->getFixtures(), true);
```
## Sharing objects between fixtures
In case if fixture objects have relations to other fixtures, it is now possible
to easily add a reference to that object by name and later reference it to form
a relation. Here is an example fixtures for **Role** and **User** relation
```php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
class UserRoleDataLoader extends AbstractFixture
{
public function load(ObjectManager $manager)
{
$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);
}
}
```
And the **User** data loading fixture:
```php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
class UserDataLoader extends AbstractFixture
{
public function load(ObjectManager $manager)
{
$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);
}
}
```
## Fixture ordering
**Notice** that the fixture loading order is important! To handle it manually
implement one of the following interfaces:
### OrderedFixtureInterface
Set the order manually:
```php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class MyFixture extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{}
public function getOrder()
{
return 10; // number in which order to load fixtures
}
}
```
### DependentFixtureInterface
Provide an array of fixture class names:
```php
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class MyFixture extends AbstractFixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager)
{}
public function getDependencies()
{
return array('MyDataFixtures\MyOtherFixture'); // fixture classes fixture is dependent on
}
}
class MyOtherFixture extends AbstractFixture
{
public function load(ObjectManager $manager)
{}
}
```
**Notice** the ordering is relevant to Loader class.
## Running the tests:
Phpunit is included in the dev requirements of this package.
To setup and run tests follow these steps:
- go to the root directory of data-fixtures
- run: **composer install --dev**
- run: **vendor/bin/phpunit**