|
58
|
1 |
DoctrineFixturesBundle |
|
|
2 |
====================== |
|
|
3 |
|
|
|
4 |
Fixtures are used to load a controlled set of data into a database. This data |
|
|
5 |
can be used for testing or could be the initial data required for the |
|
|
6 |
application to run smoothly. Symfony2 has no built in way to manage fixtures |
|
|
7 |
but Doctrine2 has a library to help you write fixtures for the Doctrine |
|
|
8 |
:doc:`ORM</book/doctrine>` or :doc:`ODM</bundles/DoctrineMongoDBBundle/index>`. |
|
|
9 |
|
|
|
10 |
Setup and Configuration |
|
|
11 |
----------------------- |
|
|
12 |
|
|
|
13 |
If you don't have the `Doctrine Data Fixtures`_ library configured with Symfony2 |
|
|
14 |
yet, follow these steps to do so. |
|
|
15 |
|
|
|
16 |
If you're using the Standard Distribution, add the following to your ``deps`` |
|
|
17 |
file: |
|
|
18 |
|
|
|
19 |
.. code-block:: text |
|
|
20 |
|
|
|
21 |
[doctrine-fixtures] |
|
|
22 |
git=http://github.com/doctrine/data-fixtures.git |
|
|
23 |
|
|
|
24 |
[DoctrineFixturesBundle] |
|
|
25 |
git=http://github.com/symfony/DoctrineFixturesBundle.git |
|
|
26 |
target=/bundles/Symfony/Bundle/DoctrineFixturesBundle |
|
|
27 |
|
|
|
28 |
Update the vendor libraries: |
|
|
29 |
|
|
|
30 |
.. code-block:: bash |
|
|
31 |
|
|
|
32 |
$ php bin/vendors install |
|
|
33 |
|
|
|
34 |
If everything worked, the ``doctrine-fixtures`` library can now be found |
|
|
35 |
at ``vendor/doctrine-fixtures``. |
|
|
36 |
|
|
|
37 |
Register the ``Doctrine\Common\DataFixtures`` namespace in ``app/autoload.php``. |
|
|
38 |
|
|
|
39 |
.. code-block:: php |
|
|
40 |
|
|
|
41 |
// ... |
|
|
42 |
$loader->registerNamespaces(array( |
|
|
43 |
// ... |
|
|
44 |
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib', |
|
|
45 |
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib', |
|
|
46 |
// ... |
|
|
47 |
)); |
|
|
48 |
|
|
|
49 |
.. caution:: |
|
|
50 |
|
|
|
51 |
Be sure to register the new namespace *before* ``Doctrine\Common``. Otherwise, |
|
|
52 |
Symfony will look for data fixture classes inside the ``Doctrine\Common`` |
|
|
53 |
directory. Symfony's autoloader always looks for a class inside the directory |
|
|
54 |
of the first matching namespace, so more specific namespaces should always |
|
|
55 |
come first. |
|
|
56 |
|
|
|
57 |
Finally, register the Bundle ``DoctrineFixturesBundle`` in ``app/AppKernel.php``. |
|
|
58 |
|
|
|
59 |
.. code-block:: php |
|
|
60 |
|
|
|
61 |
// ... |
|
|
62 |
public function registerBundles() |
|
|
63 |
{ |
|
|
64 |
$bundles = array( |
|
|
65 |
// ... |
|
|
66 |
new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(), |
|
|
67 |
// ... |
|
|
68 |
); |
|
|
69 |
// ... |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
Writing Simple Fixtures |
|
|
73 |
----------------------- |
|
|
74 |
|
|
|
75 |
Doctrine2 fixtures are PHP classes where you can create objects and persist |
|
|
76 |
them to the database. Like all classes in Symfony2, fixtures should live inside |
|
|
77 |
one of your application bundles. |
|
|
78 |
|
|
|
79 |
For a bundle located at ``src/Acme/HelloBundle``, the fixture classes |
|
|
80 |
should live inside ``src/Acme/HelloBundle/DataFixtures/ORM`` or |
|
|
81 |
``src/Acme/HelloBundle/DataFixtures/MongoDB`` respectively for the ORM and ODM, |
|
|
82 |
This tutorial assumes that you are using the ORM - but fixtures can be added |
|
|
83 |
just as easily if you're using the ODM. |
|
|
84 |
|
|
|
85 |
Imagine that you have a ``User`` class, and you'd like to load one ``User`` |
|
|
86 |
entry: |
|
|
87 |
|
|
|
88 |
.. code-block:: php |
|
|
89 |
|
|
|
90 |
// src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php |
|
|
91 |
namespace Acme\HelloBundle\DataFixtures\ORM; |
|
|
92 |
|
|
|
93 |
use Doctrine\Common\DataFixtures\FixtureInterface; |
|
|
94 |
use Acme\HelloBundle\Entity\User; |
|
|
95 |
|
|
|
96 |
class LoadUserData implements FixtureInterface |
|
|
97 |
{ |
|
|
98 |
public function load($manager) |
|
|
99 |
{ |
|
|
100 |
$userAdmin = new User(); |
|
|
101 |
$userAdmin->setUsername('admin'); |
|
|
102 |
$userAdmin->setPassword('test'); |
|
|
103 |
|
|
|
104 |
$manager->persist($userAdmin); |
|
|
105 |
$manager->flush(); |
|
|
106 |
} |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
In Doctrine2, fixtures are just objects where you load data by interacting |
|
|
110 |
with your entities as you normally do. This allows you to create the exact |
|
|
111 |
fixtures you need for your application. |
|
|
112 |
|
|
|
113 |
The most serious limitation is that you cannot share objects between fixtures. |
|
|
114 |
Later, you'll see how to overcome this limitation. |
|
|
115 |
|
|
|
116 |
Executing Fixtures |
|
|
117 |
------------------ |
|
|
118 |
|
|
|
119 |
Once your fixtures have been written, you can load them via the command |
|
|
120 |
line by using the ``doctrine:fixtures:load`` command: |
|
|
121 |
|
|
|
122 |
.. code-block:: bash |
|
|
123 |
|
|
|
124 |
php app/console doctrine:fixtures:load |
|
|
125 |
|
|
|
126 |
If you're using the ODM, use the ``doctrine:mongodb:fixtures:load`` command instead: |
|
|
127 |
|
|
|
128 |
.. code-block:: bash |
|
|
129 |
|
|
|
130 |
php app/console doctrine:mongodb:fixtures:load |
|
|
131 |
|
|
|
132 |
The task will look inside the ``DataFixtures/ORM`` (or ``DataFixtures/MongoDB`` |
|
|
133 |
for the ODM) directory of each bundle and execute each class that implements |
|
|
134 |
the ``FixtureInterface``. |
|
|
135 |
|
|
|
136 |
Both commands come with a few options: |
|
|
137 |
|
|
|
138 |
* ``--fixtures=/path/to/fixture`` - Use this option to manually specify the |
|
|
139 |
directory where the fixtures classes should be loaded; |
|
|
140 |
|
|
|
141 |
* ``--append`` - Use this flag to append data instead of deleting data before |
|
|
142 |
loading it (deleting first is the default behavior); |
|
|
143 |
|
|
|
144 |
* ``--em=manager_name`` - Manually specify the entity manager to use for |
|
|
145 |
loading the data. |
|
|
146 |
|
|
|
147 |
.. note:: |
|
|
148 |
|
|
|
149 |
If using the ``doctrine:mongodb:fixtures:load`` task, replace the ``--em=`` |
|
|
150 |
option with ``--dm=`` to manually specify the document manager. |
|
|
151 |
|
|
|
152 |
A full example use might look like this: |
|
|
153 |
|
|
|
154 |
.. code-block:: bash |
|
|
155 |
|
|
|
156 |
php app/console doctrine:fixtures:load --fixtures=/path/to/fixture1 --fixtures=/path/to/fixture2 --append --em=foo_manager |
|
|
157 |
|
|
|
158 |
Sharing Objects between Fixtures |
|
|
159 |
-------------------------------- |
|
|
160 |
|
|
|
161 |
Writing a basic fixture is simple. But what if you have multiple fixture classes |
|
|
162 |
and want to be able to refer to the data loaded in other fixture classes? |
|
|
163 |
For example, what if you load a ``User`` object in one fixture, and then |
|
|
164 |
want to refer to reference it in a different fixture in order to assign that |
|
|
165 |
user to a particular group? |
|
|
166 |
|
|
|
167 |
The Doctrine fixtures library handles this easily by allowing you to specify |
|
|
168 |
the order in which fixtures are loaded. |
|
|
169 |
|
|
|
170 |
.. code-block:: php |
|
|
171 |
|
|
|
172 |
// src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php |
|
|
173 |
namespace Acme\HelloBundle\DataFixtures\ORM; |
|
|
174 |
|
|
|
175 |
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
|
176 |
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
|
|
177 |
use Acme\HelloBundle\Entity\User; |
|
|
178 |
|
|
|
179 |
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface |
|
|
180 |
{ |
|
|
181 |
public function load($manager) |
|
|
182 |
{ |
|
|
183 |
$userAdmin = new User(); |
|
|
184 |
$userAdmin->setUsername('admin'); |
|
|
185 |
$userAdmin->setPassword('test'); |
|
|
186 |
|
|
|
187 |
$manager->persist($userAdmin); |
|
|
188 |
$manager->flush(); |
|
|
189 |
|
|
|
190 |
$this->addReference('admin-user', $userAdmin); |
|
|
191 |
} |
|
|
192 |
|
|
|
193 |
public function getOrder() |
|
|
194 |
{ |
|
|
195 |
return 1; // the order in which fixtures will be loaded |
|
|
196 |
} |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
The fixture class now implements ``OrderedFixtureInterface``, which tells |
|
|
200 |
Doctrine that you want to control the order of your fixtures. Create another |
|
|
201 |
fixture class and make it load after ``LoadUserData`` by returning an order |
|
|
202 |
of 2: |
|
|
203 |
|
|
|
204 |
.. code-block:: php |
|
|
205 |
|
|
|
206 |
// src/Acme/HelloBundle/DataFixtures/ORM/LoadGroupData.php |
|
|
207 |
namespace Acme\HelloBundle\DataFixtures\ORM; |
|
|
208 |
|
|
|
209 |
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
|
210 |
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
|
|
211 |
use Acme\HelloBundle\Entity\Group; |
|
|
212 |
|
|
|
213 |
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface |
|
|
214 |
{ |
|
|
215 |
public function load($manager) |
|
|
216 |
{ |
|
|
217 |
$groupAdmin = new Group(); |
|
|
218 |
$groupAdmin->setGroupName('admin'); |
|
|
219 |
|
|
|
220 |
$manager->persist($groupAdmin); |
|
|
221 |
$manager->flush(); |
|
|
222 |
|
|
|
223 |
$this->addReference('admin-group', $groupAdmin); |
|
|
224 |
} |
|
|
225 |
|
|
|
226 |
public function getOrder() |
|
|
227 |
{ |
|
|
228 |
return 2; // the order in which fixtures will be loaded |
|
|
229 |
} |
|
|
230 |
} |
|
|
231 |
|
|
|
232 |
Both of the fixture classes extend ``AbstractFixture``, which allows you |
|
|
233 |
to create objects and then set them as references so that they can be used |
|
|
234 |
later in other fixtures. For example, the ``$userAdmin`` and ``$groupAdmin`` |
|
|
235 |
objects can be referenced later via the ``admin-user`` and ``admin-group`` |
|
|
236 |
references: |
|
|
237 |
|
|
|
238 |
.. code-block:: php |
|
|
239 |
|
|
|
240 |
// src/Acme/HelloBundle/DataFixtures/ORM/LoadUserGroupData.php |
|
|
241 |
namespace Acme\HelloBundle\DataFixtures\ORM; |
|
|
242 |
|
|
|
243 |
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
|
244 |
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
|
|
245 |
use Acme\HelloBundle\Entity\UserGroup; |
|
|
246 |
|
|
|
247 |
class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface |
|
|
248 |
{ |
|
|
249 |
public function load($manager) |
|
|
250 |
{ |
|
|
251 |
$userGroupAdmin = new UserGroup(); |
|
|
252 |
$userGroupAdmin->setUser($manager->merge($this->getReference('admin-user'))); |
|
|
253 |
$userGroupAdmin->setGroup($manager->merge($this->getReference('admin-group'))); |
|
|
254 |
|
|
|
255 |
$manager->persist($userGroupAdmin); |
|
|
256 |
$manager->flush(); |
|
|
257 |
} |
|
|
258 |
|
|
|
259 |
public function getOrder() |
|
|
260 |
{ |
|
|
261 |
return 3; |
|
|
262 |
} |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
The fixtures will now be executed in the ascending order of the value returned |
|
|
266 |
by ``getOrder()``. Any object that is set with the ``setReference()`` method |
|
|
267 |
can be accessed via ``getReference()`` in fixture classes that have a higher |
|
|
268 |
order. |
|
|
269 |
|
|
|
270 |
Fixtures allow you to create any type of data you need via the normal PHP |
|
|
271 |
interface for creating and persisting objects. By controlling the order of |
|
|
272 |
fixtures and setting references, almost anything can be handled by fixtures. |
|
|
273 |
|
|
|
274 |
Using the Container in the Fixtures |
|
|
275 |
----------------------------------- |
|
|
276 |
|
|
|
277 |
In some cases you may need to access some services to load the fixtures. |
|
|
278 |
Symfony2 makes it really easy: the container will be injected in all fixture |
|
|
279 |
classes implementing :class:`Symfony\\Component\\DependencyInjection\\ContainerAwareInterface`. |
|
|
280 |
|
|
|
281 |
Let's rewrite the first fixture to encode the password before it's stored |
|
|
282 |
in the database (a very good practice). This will use the encoder factory |
|
|
283 |
to encode the password, ensuring it is encoded in the way used by the security |
|
|
284 |
component when checking it: |
|
|
285 |
|
|
|
286 |
.. code-block:: php |
|
|
287 |
|
|
|
288 |
// src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php |
|
|
289 |
namespace Acme\HelloBundle\DataFixtures\ORM; |
|
|
290 |
|
|
|
291 |
use Doctrine\Common\DataFixtures\FixtureInterface; |
|
|
292 |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
|
293 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
294 |
use Acme\HelloBundle\Entity\User; |
|
|
295 |
|
|
|
296 |
class LoadUserData implements FixtureInterface, ContainerAwareInterface |
|
|
297 |
{ |
|
|
298 |
private $container; |
|
|
299 |
|
|
|
300 |
public function setContainer(ContainerInterface $container = null) |
|
|
301 |
{ |
|
|
302 |
$this->container = $container; |
|
|
303 |
} |
|
|
304 |
|
|
|
305 |
public function load($manager) |
|
|
306 |
{ |
|
|
307 |
$userAdmin = new User(); |
|
|
308 |
$userAdmin->setUsername('admin'); |
|
|
309 |
$userAdmin->setSalt(md5(time())); |
|
|
310 |
|
|
|
311 |
$encoder = $this->container->get('security.encoder_factory')->getEncoder($userAdmin); |
|
|
312 |
$userAdmin->setPassword($encoder->encodePassword('test', $userAdmin->getSalt())); |
|
|
313 |
|
|
|
314 |
$manager->persist($userAdmin); |
|
|
315 |
$manager->flush(); |
|
|
316 |
} |
|
|
317 |
} |
|
|
318 |
|
|
|
319 |
As you can see, all you need to do is add ``ContainerAwareInterface`` to |
|
|
320 |
the class and then create a new ``setContainer()`` method that implements |
|
|
321 |
that interface. Before the fixture is executed, Symfony will call the ``setContainer()`` |
|
|
322 |
method automatically. As long as you store the container as a property on |
|
|
323 |
the class (as shown above), you can access it in the ``load()`` method. |
|
|
324 |
|
|
|
325 |
.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures |