Skip to content

Commit 2cacc37

Browse files
committed
a little more on how to use fixtures
1 parent c7468d8 commit 2cacc37

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed
Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,98 @@
1-
Doctrine2 Fixtures with Symfony2
2-
=========================
1+
.. index::
2+
single: Doctrine; Creating fixtures in Symfony2
33

4-
##Setup and Configuration
4+
How to create fixtures in Symfony2
5+
==================================
56

6-
If you don't have Doctrine data-fixtures configured with Symfony2 follow the steps here.
7+
Fixtures are used to load the database with a set of data. This data can either be for testing or could be the initial data required for the application to run smoothly. Symfony2 has no built in way to manage fixtures but Doctrine2 has a library to help you write fixtures for ORM and ODM.
78

8-
Add the following to `bin/vendors.sh`
9+
Setup and Configuration
10+
-----------------------
11+
12+
If you don't have `Doctrine Data Fixtures`_ configured with Symfony2 yes, follow these steps to do so.
13+
14+
Add the following to ``bin/vendors.sh``
15+
16+
.. code-block:: bash
917
1018
#Doctrine Fixtures
1119
install_git doctrine-fixtures git://github.com/doctrine/data-fixtures.git
1220
1321
Update vendors and rebuild the bootstrap file
1422

23+
.. code-block:: bash
24+
1525
bin/vendors.sh
1626
bin/build_bootstrap.php
1727
18-
As a final step in configuration, you have to register the namespace in `app/autoload.php`
28+
As the final step in configuration, you have to register the namespace in ``app/autoload.php``
29+
30+
.. code-block:: php
1931
2032
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
2133
22-
##Simple Fixtures
34+
Simple Fixtures
35+
---------------
36+
37+
The ideal place to store your fixtures is inside ``Vendor/MyBundle/DataFixtures/ORM`` and ``Vendor/MyBundle/DataFixtures/ODM`` respectively for ORM and ODM.
2338

24-
`TODO: Add a simple fixture, ORM and ODM`
39+
In this tutorial we will assume you are using ORM. If you are using ODM make the changes as required.
2540

26-
##Sharing Objects Between Fixtures
41+
In our first fixture we will add a default user to the table of ``User`` entity.
2742

28-
#Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php
43+
.. code-block:: php
2944
3045
<?php
46+
47+
//Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php
48+
namespace Vendor\MyBundle\DataFixtures\ORM;
49+
50+
use Doctrine\ORM\EntityManager;
51+
use Doctrine\Common\DataFixtures\FixtureInterface;
52+
use Vendor\MyBundle\Entity\User; //Modify this to use your entity
53+
54+
class LoadUserData implements FixtureInterface
55+
{
56+
public function load($manager)
57+
{
58+
$userAdmin = new User();
59+
$userAdmin->setUsername('admin');
60+
$userAdmin->setPassword('test');
3161
62+
$manager->persist($userAdmin);
63+
$manager->flush();
64+
}
65+
}
66+
67+
Writing fixtures this way is quite easy and simple but is not sufficient when you are building something serious. The most serious limitation is that you can not share objects between fixtures. Lets see how we can overcome this limitation in the next section.
68+
69+
Sharing Objects Between Fixtures
70+
--------------------------------
71+
72+
.. code-block:: php
73+
74+
<?php
75+
76+
//Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php
3277
namespace Vendor\MyBundle\DataFixtures\ORM;
3378
34-
use Vendor\MyBundle\Entity\User;
3579
use Doctrine\ORM\EntityManager;
3680
use Doctrine\Common\DataFixtures\AbstractFixture;
3781
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
82+
use Vendor\MyBundle\Entity\User; //Modify this to use your entity
3883
3984
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
4085
{
4186
public function load($manager)
4287
{
43-
$userKertz = new User();
44-
$userKertz->setUsername('kertz');
45-
$userKertz->setPassword('test');
88+
$userAdmin = new User();
89+
$userAdmin->setUsername('admin');
90+
$userAdmin->setPassword('test');
4691
47-
$manager->persist($userKertz);
92+
$manager->persist($userAdmin);
4893
$manager->flush();
4994
50-
$this->addReference('kertz-user', $userKertz);
95+
$this->addReference('admin-user', $userAdmin);
5196
}
5297
5398
public function getOrder()
@@ -57,17 +102,17 @@ As a final step in configuration, you have to register the namespace in `app/aut
57102
}
58103
59104
60-
61-
#Vendor/MyBundle/DataFixtures/ORM/LoadGroupData.php
105+
.. code-block:: php
62106
63107
<?php
64-
108+
109+
//Vendor/MyBundle/DataFixtures/ORM/LoadGroupData.php
65110
namespace Vendor\MyBundle\DataFixtures\ORM;
66111
67-
use Vendor\MyBundle\Entity\Group;
68112
use Doctrine\ORM\EntityManager;
69113
use Doctrine\Common\DataFixtures\AbstractFixture;
70114
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
115+
use Vendor\MyBundle\Entity\Group; //Modify this to use your entity
71116
72117
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
73118
{
@@ -88,30 +133,30 @@ As a final step in configuration, you have to register the namespace in `app/aut
88133
}
89134
}
90135
91-
92-
#Vendor/MyBundle/DataFixtures/ORM/LoadUserGroupData.php
136+
.. code-block:: php
93137
94138
<?php
95-
139+
140+
//Vendor/MyBundle/DataFixtures/ORM/LoadUserGroupData.php
96141
namespace Vendor\MyBundle\DataFixtures\ORM;
97142
98-
use Vendor\MyBundle\Entity\UserGroup;
99143
use Doctrine\ORM\EntityManager;
100144
use Doctrine\Common\DataFixtures\AbstractFixture;
101145
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
102-
146+
use Vendor\MyBundle\Entity\UserGroup; //Modify this to use your entity
147+
103148
class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface
104149
{
105150
public function load($manager)
106151
{
107-
$manager->persist($this->getReference($userKertz));
108-
$manager->persist($this->getReference($groupAdmin));
152+
$manager->persist($this->getReference('admin-user'));
153+
$manager->persist($this->getReference('admin-group'));
109154
110-
$kertzGroup = new UserGroup();
111-
$kertzGroup->setUser($this->getReference($userKertz));
112-
$kertzGroup->setGroup($this->getReference($groupAdmin));
155+
$userGroupAdmin = new UserGroup();
156+
$userGroupAdmin->setUser($this->getReference('admin-user'));
157+
$userGroupAdmin->setGroup($this->getReference('admin-group'));
113158
114-
$manager->persist($kertzGroup);
159+
$manager->persist($userGroupAdmin);
115160
$manager->flush();
116161
}
117162
@@ -123,4 +168,8 @@ As a final step in configuration, you have to register the namespace in `app/aut
123168
124169
A brief explanation on how this works.
125170

126-
The fixtures will be executed in the order of the value returned in getOrder().
171+
The fixtures will be executed in the ascending order of the value returned by ``getOrder()``.
172+
Any object that is set with the ``setReference`` method and can be accessed with ``getReference`` in fixtures, which are of higher order.
173+
Also remember that, in order to use a reference with the entity manager, it has to first persisted.
174+
175+
.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures

cookbook/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Cookbook
2525
request/mime_type
2626
security/voters
2727
doctrine/reverse_engineering
28+
doctrine/doctrine_fixtures
2829
symfony1
2930

3031
.. include:: map.rst.inc

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
* :doc:`/cookbook/request/mime_type`
2020
* :doc:`/cookbook/security/voters`
2121
* :doc:`/cookbook/doctrine/reverse_engineering`
22+
* :doc:`/cookbook/doctrine/doctrine_fixtures`
2223
* :doc:`/cookbook/symfony1`

0 commit comments

Comments
 (0)