4
4
How to create Fixtures in Symfony2
5
5
==================================
6
6
7
- Fixtures are used to load the database with a set of data. This data can
8
- either be for testing or could be the initial data required for the
7
+ Fixtures are used to load a controlled set of data into a database. This
8
+ data can be used for testing or could be the initial data required for the
9
9
application to run smoothly. Symfony2 has no built in way to manage fixtures
10
10
but Doctrine2 has a library to help you write fixtures for the Doctrine
11
11
:doc: `ORM</book/doctrine/orm/overview> ` or :doc: `ODM</book/doctrine/mongodb-odm/overview> `.
@@ -53,10 +53,15 @@ come first.
53
53
Writing Simple Fixtures
54
54
-----------------------
55
55
56
- The ideal place to store your fixtures is inside
57
- ``src/VendorName/MyBundle/DataFixtures/ORM `` and ``src/VendorName/MyBundle/DataFixtures/ODM ``
58
- respectively for the ORM and ODM. This tutorial assumes that you are using
59
- the ORM - but fixtures can be added just as easily if you're using the ODM.
56
+ Doctrine2 fixtures are PHP classes where you can create objects and persist
57
+ them to the database. Like all classes in Symfony2, fixtures should live inside
58
+ one of your application bundles.
59
+
60
+ For a bundle located at ``src/VendorName/MyBundle ``, the fixture classes
61
+ should live inside ``src/VendorName/MyBundle/DataFixtures/ORM `` or
62
+ ``src/VendorName/MyBundle/DataFixtures/ODM `` respectively for the ORM and ODM,
63
+ This tutorial assumes that you are using the ORM - but fixtures can be added
64
+ just as easily if you're using the ODM.
60
65
61
66
Imagine that you have a ``User `` class, and you'd like to load one ``User ``
62
67
entry:
@@ -85,17 +90,17 @@ entry:
85
90
}
86
91
87
92
In Doctrine2, fixtures are just objects where you load data by interacting
88
- with your entities as you normal do. This allows you to create the exact
89
- fixtures you want for your application.
93
+ with your entities as you normally do. This allows you to create the exact
94
+ fixtures you need for your application.
90
95
91
- The most serious limitation is that you can not share objects between fixtures.
96
+ The most serious limitation is that you cannot share objects between fixtures.
92
97
Later, you'll see how to overcome this limitation.
93
98
94
99
Executing Fixtures
95
100
------------------
96
101
97
- Once your fixtures have been written, you load the fixtures via the command
98
- line via the ``doctrine:data:load `` command:
102
+ Once your fixtures have been written, you can load them via the command
103
+ line by using the ``doctrine:data:load `` command:
99
104
100
105
.. code-block :: bash
101
106
@@ -117,7 +122,7 @@ Both commands come with a few options:
117
122
directory or file where the fixtures classes should be loaded;
118
123
119
124
* ``--append `` - Use this flag to append data instead of deleting data before
120
- loading it (the default behavior);
125
+ loading it (deleting first is the default behavior);
121
126
122
127
* ``--em=manager_name `` - Manually specify the entity manager to use for
123
128
loading the data.
@@ -127,7 +132,7 @@ Both commands come with a few options:
127
132
If using the ``doctrine:mongodb:data:load `` task, replace the ``--em= ``
128
133
option with ``--dm= `` to manually specify the document manager.
129
134
130
- A full example use might look like:
135
+ A full example use might look like this :
131
136
132
137
.. code-block :: bash
133
138
@@ -136,16 +141,23 @@ A full example use might look like:
136
141
Sharing Objects between Fixtures
137
142
--------------------------------
138
143
144
+ Writing a basic fixture is simple. But what if you have multiple fixture classes
145
+ and want to be able to refer to the data loaded in other fixture classes?
146
+ For example, what if you load a ``User `` object in one fixture, and then
147
+ want to refer to reference it in a different fixture in order to assign that
148
+ user to a particular group?
149
+
150
+ The Doctrine fixtures library handles this easily by allowing you to specify
151
+ the order in which fixtures are loaded.
152
+
139
153
.. code-block :: php
140
154
141
- <?php
142
-
143
- //Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php
144
- namespace Vendor\MyBundle\DataFixtures\ORM;
155
+ // src/VendorName/MyBundle/DataFixtures/ORM/LoadUserData.php
156
+ namespace VendorName\MyBundle\DataFixtures\ORM;
145
157
146
158
use Doctrine\Common\DataFixtures\AbstractFixture;
147
159
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
148
- use Vendor \MyBundle\Entity\User; //Modify this to use your entity
160
+ use VendorName \MyBundle\Entity\User;
149
161
150
162
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
151
163
{
@@ -167,17 +179,19 @@ Sharing Objects between Fixtures
167
179
}
168
180
}
169
181
182
+ The fixture class now implements ``OrderedFixtureInterface ``, which tells
183
+ Doctrine that you want to control the order of your fixtures. Create another
184
+ fixture class and make it load after ``LoadUserData `` by returning an order
185
+ of 2:
170
186
171
187
.. code-block :: php
172
188
173
- <?php
174
-
175
- //Vendor/MyBundle/DataFixtures/ORM/LoadGroupData.php
176
- namespace Vendor\MyBundle\DataFixtures\ORM;
189
+ // src/VendorName/MyBundle/DataFixtures/ORM/LoadGroupData.php
190
+ namespace VendorName\MyBundle\DataFixtures\ORM;
177
191
178
192
use Doctrine\Common\DataFixtures\AbstractFixture;
179
193
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
180
- use Vendor \MyBundle\Entity\Group; //Modify this to use your entity
194
+ use VendorName \MyBundle\Entity\Group;
181
195
182
196
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
183
197
{
@@ -198,16 +212,20 @@ Sharing Objects between Fixtures
198
212
}
199
213
}
200
214
215
+ Both of the fixture classes extend ``AbstractFixture ``, which allows you
216
+ to create objects and then set them as references so that they can be used
217
+ later in other fixtures. For example, the ``$userAdmin `` and ``$groupAdmin ``
218
+ objects can be referenced later via the ``admin-user `` and ``admin-group ``
219
+ references:
220
+
201
221
.. code-block :: php
202
222
203
- <?php
204
-
205
- //Vendor/MyBundle/DataFixtures/ORM/LoadUserGroupData.php
206
- namespace Vendor\MyBundle\DataFixtures\ORM;
223
+ // src/VendorName/MyBundle/DataFixtures/ORM/LoadUserGroupData.php
224
+ namespace VendorName\MyBundle\DataFixtures\ORM;
207
225
208
226
use Doctrine\Common\DataFixtures\AbstractFixture;
209
227
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
210
- use Vendor \MyBundle\Entity\UserGroup; //Modify this to use your entity
228
+ use VendorName \MyBundle\Entity\UserGroup;
211
229
212
230
class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface
213
231
{
@@ -223,14 +241,17 @@ Sharing Objects between Fixtures
223
241
224
242
public function getOrder()
225
243
{
226
- return 3; // the order in which fixtures will be loaded
244
+ return 3;
227
245
}
228
246
}
229
247
230
- A brief explanation on how this works.
248
+ The fixtures will now be executed in the ascending order of the value returned
249
+ by ``getOrder() ``. Any object that is set with the ``setReference() `` method
250
+ can be accessed via ``getReference() `` in fixture classes that have a higher
251
+ order.
231
252
232
- The fixtures will be executed in the ascending order of the value returned by
233
- `` getOrder() ``. Any object that is set with the `` setReference `` method and
234
- can be accessed via `` getReference `` in fixtures, which are of higher order .
253
+ Fixtures allow you to create any type of data you need via the normal PHP
254
+ interface for creating and persisting objects. By controlling the order of
255
+ fixtures and setting references, almost anything can be handled by fixtures .
235
256
236
- .. _`Doctrine Data Fixtures` : https://github.com/doctrine/data-fixtures
257
+ .. _`Doctrine Data Fixtures` : https://github.com/doctrine/data-fixtures
0 commit comments