|
58
|
1 |
# Doctrine Data Fixtures Extension |
|
|
2 |
|
|
|
3 |
This extension aims to provide a simple way to manage and execute the loading of data fixtures |
|
|
4 |
for the Doctrine ORM or ODM. You can write fixture classes by implementing the |
|
|
5 |
Doctrine\Common\DataFixtures\FixtureInterface interface: |
|
|
6 |
|
|
|
7 |
namespace MyDataFixtures; |
|
|
8 |
|
|
|
9 |
use Doctrine\ORM\EntityManager; |
|
|
10 |
use Doctrine\Common\DataFixtures\FixtureInterface; |
|
|
11 |
|
|
|
12 |
class LoadUserData implements FixtureInterface |
|
|
13 |
{ |
|
|
14 |
public function load($manager) |
|
|
15 |
{ |
|
|
16 |
$user = new User(); |
|
|
17 |
$user->setUsername('jwage'); |
|
|
18 |
$user->setPassword('test'); |
|
|
19 |
|
|
|
20 |
$manager->persist($user); |
|
|
21 |
$manager->flush(); |
|
|
22 |
} |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
Now you can begin adding the fixtures to a loader instance: |
|
|
26 |
|
|
|
27 |
use Doctrine\Common\DataFixtures\Loader; |
|
|
28 |
use MyDataFixtures\LoadUserData; |
|
|
29 |
|
|
|
30 |
$loader = new Loader(); |
|
|
31 |
$loader->addFixture(new LoadUserData); |
|
|
32 |
|
|
|
33 |
You can load a set of fixtures from a directory as well: |
|
|
34 |
|
|
|
35 |
$loader->loadFromDirectory('/path/to/MyDataFixtures'); |
|
|
36 |
|
|
|
37 |
You can get the added fixtures using the getFixtures() method: |
|
|
38 |
|
|
|
39 |
$fixtures = $loader->getFixtures(); |
|
|
40 |
|
|
|
41 |
Now you can easily execute the fixtures: |
|
|
42 |
|
|
|
43 |
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
|
|
44 |
|
|
|
45 |
$purger = new Purger(); |
|
|
46 |
$executor = new ORMExecutor($em, $purger); |
|
|
47 |
$executor->execute($loader->getFixtures()); |
|
|
48 |
|
|
|
49 |
If you want to append the fixtures instead of purging before loading then pass false |
|
|
50 |
to the 2nd argument of execute: |
|
|
51 |
|
|
|
52 |
$executor->execute($loader->getFixtures(), true); |
|
|
53 |
|
|
|
54 |
## Sharing objects between fixtures |
|
|
55 |
|
|
|
56 |
In case if fixture objects have relations to other fixtures, it is now possible |
|
|
57 |
to easily add a reference to that object by name and later reference it to form |
|
|
58 |
a relation. Here is an example fixtures for **Role** and **User** relation |
|
|
59 |
|
|
|
60 |
namespace MyDataFixtures; |
|
|
61 |
|
|
|
62 |
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
|
63 |
|
|
|
64 |
class LoadUserRoleData extends AbstractFixture |
|
|
65 |
{ |
|
|
66 |
public function load($manager) |
|
|
67 |
{ |
|
|
68 |
$adminRole = new Role(); |
|
|
69 |
$adminRole->setName('admin'); |
|
|
70 |
|
|
|
71 |
$anonymousRole = new Role; |
|
|
72 |
$anonymousRole->setName('anonymous'); |
|
|
73 |
|
|
|
74 |
$manager->persist($adminRole); |
|
|
75 |
$manager->persist($anonymousRole); |
|
|
76 |
$manager->flush(); |
|
|
77 |
|
|
|
78 |
// store reference to admin role for User relation to Role |
|
|
79 |
$this->addReference('admin-role', $adminRole); |
|
|
80 |
} |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
And the **User** data loading fixture: |
|
|
84 |
|
|
|
85 |
namespace MyDataFixtures; |
|
|
86 |
|
|
|
87 |
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
|
88 |
|
|
|
89 |
class LoadUserData extends AbstractFixture |
|
|
90 |
{ |
|
|
91 |
public function load($manager) |
|
|
92 |
{ |
|
|
93 |
$user = new User(); |
|
|
94 |
$user->setUsername('jwage'); |
|
|
95 |
$user->setPassword('test'); |
|
|
96 |
$user->setRole( |
|
|
97 |
$this->getReference('admin-role') // load the stored reference |
|
|
98 |
); |
|
|
99 |
|
|
|
100 |
$manager->persist($user); |
|
|
101 |
$manager->flush(); |
|
|
102 |
|
|
|
103 |
// store reference of admin-user for other Fixtures |
|
|
104 |
$this->addReference('admin-user', $user); |
|
|
105 |
} |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
**Notice** that the fixture loading order is important! To handle it manually |
|
|
109 |
implement the OrderedFixtureInterface and set the order: |
|
|
110 |
|
|
|
111 |
namespace MyDataFixtures; |
|
|
112 |
|
|
|
113 |
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
|
114 |
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
|
|
115 |
|
|
|
116 |
class MyFixture extends AbstractFixture implements OrderedFixtureInterface |
|
|
117 |
{ |
|
|
118 |
public function load($manager) |
|
|
119 |
{} |
|
|
120 |
|
|
|
121 |
public function getOrder() |
|
|
122 |
{ |
|
|
123 |
return 10; // number in which order to load fixtures |
|
|
124 |
} |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
**Notice** the ordering is relevant to Loader class. |
|
|
128 |
|
|
|
129 |
## Running the tests: |
|
|
130 |
|
|
|
131 |
PHPUnit 3.5 or newer together with Mock_Object package is required. |
|
|
132 |
To setup and run tests follow these steps: |
|
|
133 |
|
|
|
134 |
- go to the root directory of data-fixtures |
|
|
135 |
- run: **git submodule init** |
|
|
136 |
- run: **git submodule update** |
|
|
137 |
- copy the phpunit config **cp phpunit.xml.dist phpunit.xml** |
|
|
138 |
- run: **phpunit** |