8000 [cookbook][doctrine] Added a section about using the container in the… · chtitux/symfony-docs@c8d8087 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8d8087

Browse files
committed
[cookbook][doctrine] Added a section about using the container in the fixtures
1 parent a4ecac3 commit c8d8087

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

cookbook/doctrine/doctrine_fixtures.rst

Lines changed: 60 additions & 15 deletions
169
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ Doctrine2 fixtures are PHP classes where you can create objects and persist
7575
them to the database. Like all classes in Symfony2, fixtures should live inside
7676
one of your application bundles.
7777

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,
8181
This tutorial assumes that you are using the ORM - but fixtures can be added
8282
just as easily if you're using the ODM.
8383

@@ -86,11 +86,11 @@ entry:
8686

8787
.. code-block:: php
8888
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;
9191
9292
use Doctrine\Common\DataFixtures\FixtureInterface;
93-
use VendorName\MyBundle\Entity\User;
93+
use Acme\HelloBundle\Entity\User;
9494
9595
class LoadUserData implements FixtureInterface
9696
{
@@ -168,12 +168,12 @@ the order in which fixtures are loaded.
168168

169
.. code-block:: php
170170
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;
173173
174174
use Doctrine\Common\DataFixtures\AbstractFixture;
175175
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
176-
use VendorName\MyBundle\Entity\User;
176+
use Acme\HelloBundle\Entity\User;
177177
178178
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
179179
{
@@ -202,12 +202,12 @@ of 2:
202202

203203
.. code-block:: php
204204
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;
207207
208208
use Doctrine\Common\DataFixtures\AbstractFixture;
209209
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
210-
use VendorName\MyBundle\Entity\Group;
210+
use Acme\HelloBundle\Entity\Group;
211211
212212
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
213213
{
@@ -236,12 +236,12 @@ references:
236236

237237
.. code-block:: php
238238
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;
241241
242242
use Doctrine\Common\DataFixtures\AbstractFixture;
243243
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
244-
use VendorName\MyBundle\Entity\UserGroup;
244+
use Acme\HelloBundle\Entity\UserGroup;
245245
246246
class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface
247247
{
@@ -270,4 +270,49 @@ Fixtures allow you to create any type of data you need via the normal PHP
270270
interface for creating and persisting objects. By controlling the order of
271271
fixtures and setting references, almost anything can be handled by fixtures.
272272

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+
273318
.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures

0 commit comments

Comments
 (0)
0