@@ -75,9 +75,9 @@ Doctrine2 fixtures are PHP classes where you can create objects and persist
75
75
them to the database. Like all classes in Symfony2, fixtures should live inside
76
76
one of your application bundles.
77
77
78
- For a bundle located at ``src/VendorName/MyBundle ``, the fixture classes
79
- should live inside ``src/VendorName/MyBundle /DataFixtures/ORM `` or
80
- ``src/VendorName/MyBundle /DataFixtures/ODM `` respectively for the ORM and ODM,
78
+ For a bundle located at ``src/Acme/HelloBundle ``, the fixture classes
79
+ should live inside ``src/Acme/HelloBundle /DataFixtures/ORM `` or
80
+ ``src/Acme/HelloBundle /DataFixtures/ODM `` respectively for the ORM and ODM,
81
81
This tutorial assumes that you are using the ORM - but fixtures can be added
82
82
just as easily if you're using the ODM.
83
83
@@ -86,11 +86,11 @@ entry:
86
86
87
87
.. code-block :: php
88
88
89
- // src/VendorName/MyBundle /DataFixtures/ORM/LoadUserData.php
90
- namespace VendorName\MyBundle \DataFixtures\ORM;
89
+ // src/Acme/HelloBundle /DataFixtures/ORM/LoadUserData.php
90
+ namespace Acme\HelloBundle \DataFixtures\ORM;
91
91
92
92
use Doctrine\Common\DataFixtures\FixtureInterface;
93
- use VendorName\MyBundle \Entity\User;
93
+ use Acme\HelloBundle \Entity\User;
94
94
95
95
class LoadUserData implements FixtureInterface
96
96
{
@@ -168,12 +168,12 @@ the order in which fixtures are loaded.
168
168
169
169
.. code-block :: php
170
170
171
- // src/VendorName/MyBundle /DataFixtures/ORM/LoadUserData.php
172
- namespace VendorName\MyBundle \DataFixtures\ORM;
171
+ // src/Acme/HelloBundle /DataFixtures/ORM/LoadUserData.php
172
+ namespace Acme\HelloBundle \DataFixtures\ORM;
173
173
174
174
use Doctrine\Common\DataFixtures\AbstractFixture;
175
175
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
176
- use VendorName\MyBundle \Entity\User;
176
+ use Acme\HelloBundle \Entity\User;
177
177
178
178
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
179
179
{
@@ -202,12 +202,12 @@ of 2:
202
202
203
203
.. code-block :: php
204
204
205
- // src/VendorName/MyBundle /DataFixtures/ORM/LoadGroupData.php
206
- namespace VendorName\MyBundle \DataFixtures\ORM;
205
+ // src/Acme/HelloBundle /DataFixtures/ORM/LoadGroupData.php
206
+ namespace Acme\HelloBundle \DataFixtures\ORM;
207
207
208
208
use Doctrine\Common\DataFixtures\AbstractFixture;
209
209
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
210
- use VendorName\MyBundle \Entity\Group;
210
+ use Acme\HelloBundle \Entity\Group;
211
211
212
212
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
213
213
{
@@ -236,12 +236,12 @@ references:
236
236
237
237
.. code-block :: php
238
238
239
- // src/VendorName/MyBundle /DataFixtures/ORM/LoadUserGroupData.php
240
- namespace VendorName\MyBundle \DataFixtures\ORM;
239
+ // src/Acme/HelloBundle /DataFixtures/ORM/LoadUserGroupData.php
240
+ namespace Acme\HelloBundle \DataFixtures\ORM;
241
241
242
242
use Doctrine\Common\DataFixtures\AbstractFixture;
243
243
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
244
- use VendorName\MyBundle \Entity\UserGroup;
244
+ use Acme\HelloBundle \Entity\UserGroup;
245
245
246
246
class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface
247
247
{
@@ -270,4 +270,49 @@ Fixtures allow you to create any type of data you need via the normal PHP
270
270
interface for creating and persisting objects. By controlling the order of
271
271
fixtures and setting references, almost anything can be handled by fixtures.
272
272
273
+ Using the Container in the Fixtures
274
+ -----------------------------------
275
+
276
+ In some cases you may need to access some services to load the fixtures.
277
+ Symfony2 makes it really easy: the container will be injected in all fixture
278
+ classes implementing :class: `Symfony\\ Component\\ DependencyInjection\\ ContainerAwareInterface `.
279
+
280
+ Let's rewrite the first firxtures to encode the password sotred in the database.
281
+ This will use the encoder factory to encode the password, ensuring it is
282
+ encoded in the way used by the security component when checking it:
283
+
284
+ .. code-block :: php
285
+
286
+ // src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php
287
+ namespace Acme\HelloBundle\DataFixtures\ORM;
288
+
289
+ use Doctrine\Common\DataFixtures\FixtureInterface;
290
+ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
291
+ use Symfony\Component\DependencyInjection\ContainerInterface;
292
+ use Acme\HelloBundle\Entity\User;
293
+
294
+ class LoadUserData implements FixtureInterface, ContainerAwareInterface
295
+ {
296
+ private $container;
297
+
298
+ public function setContainer(ContainerInterface $container)
299
+ {
300
+ $this->container = $container;
301
+ }
302
+
303
+ public function load($manager)
304
+ {
305
+ $userAdmin = new User();
306
+ $userAdmin->setUsername('admin');
307
+ $userAdmin->setSalt(md5(time()));
308
+
309
+ $encoder = $this->container->get('security.encoder_factory')->getEncoder($userAdmin);
310
+ $userAdmin->setPassword($encoder->encodePassword('test', $userAdmin->getSalt()));
311
+
312
+ $manager->persist($userAdmin);
313
+ $manager->flush();
314
+ }
315
+ }
316
+
317
+
273
318
.. _`Doctrine Data Fixtures` : https://github.com/doctrine/data-fixtures
0 commit comments