Skip to content

Commit 4c75fe1

Browse files
committed
[cookbook][doctrine][fixtures] Finishing doctrine fixtures proofreading
1 parent c507987 commit 4c75fe1

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

cookbook/doctrine/doctrine_fixtures.rst

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
How to create Fixtures in Symfony2
55
==================================
66

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
99
application to run smoothly. Symfony2 has no built in way to manage fixtures
1010
but Doctrine2 has a library to help you write fixtures for the Doctrine
1111
:doc:`ORM</book/doctrine/orm/overview>` or :doc:`ODM</book/doctrine/mongodb-odm/overview>`.
@@ -53,10 +53,15 @@ come first.
5353
Writing Simple Fixtures
5454
-----------------------
5555

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.
6065

6166
Imagine that you have a ``User`` class, and you'd like to load one ``User``
6267
entry:
@@ -85,17 +90,17 @@ entry:
8590
}
8691
8792
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.
9095

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.
9297
Later, you'll see how to overcome this limitation.
9398

9499
Executing Fixtures
95100
------------------
96101

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:
99104

100105
.. code-block:: bash
101106
@@ -117,7 +122,7 @@ Both commands come with a few options:
117122
directory or file where the fixtures classes should be loaded;
118123

119124
* ``--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);
121126

122127
* ``--em=manager_name`` - Manually specify the entity manager to use for
123128
loading the data.
@@ -127,7 +132,7 @@ Both commands come with a few options:
127132
If using the ``doctrine:mongodb:data:load`` task, replace the ``--em=``
128133
option with ``--dm=`` to manually specify the document manager.
129134

130-
A full example use might look like:
135+
A full example use might look like this:
131136

132137
.. code-block:: bash
133138
@@ -136,16 +141,23 @@ A full example use might look like:
136141
Sharing Objects between Fixtures
137142
--------------------------------
138143

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+
139153
.. code-block:: php
140154
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;
145157
146158
use Doctrine\Common\DataFixtures\AbstractFixture;
147159
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
148-
use Vendor\MyBundle\Entity\User; //Modify this to use your entity
160+
use VendorName\MyBundle\Entity\User;
149161
150162
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
151163
{
@@ -167,17 +179,19 @@ Sharing Objects between Fixtures
167179
}
168180
}
169181
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:
170186

171187
.. code-block:: php
172188
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;
177191
178192
use Doctrine\Common\DataFixtures\AbstractFixture;
179193
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
180-
use Vendor\MyBundle\Entity\Group; //Modify this to use your entity
194+
use VendorName\MyBundle\Entity\Group;
181195
182196
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
183197
{
@@ -198,16 +212,20 @@ Sharing Objects between Fixtures
198212
}
199213
}
200214
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+
201221
.. code-block:: php
202222
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;
207225
208226
use Doctrine\Common\DataFixtures\AbstractFixture;
209227
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
210-
use Vendor\MyBundle\Entity\UserGroup; //Modify this to use your entity
228+
use VendorName\MyBundle\Entity\UserGroup;
211229
212230
class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface
213231
{
@@ -223,14 +241,17 @@ Sharing Objects between Fixtures
223241
224242
public function getOrder()
225243
{
226-
return 3; // the order in which fixtures will be loaded
244+
return 3;
227245
}
228246
}
229247
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.
231252

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.
235256

236-
.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures
257+
.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures

0 commit comments

Comments
 (0)