|
| 1 | +.. index:: |
| 2 | + single: Doctrine; Creating fixtures in Symfony2 |
| 3 | + |
| 4 | +How to create fixtures in Symfony2 |
| 5 | +================================== |
| 6 | + |
| 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 |
| 9 | +application to run smoothly. Symfony2 has no built in way to manage fixtures |
| 10 | +but Doctrine2 has a library to help you write fixtures for ORM and ODM. |
| 11 | + |
| 12 | +Setup and Configuration |
| 13 | +----------------------- |
| 14 | + |
| 15 | +If you don't have `Doctrine Data Fixtures`_ configured with Symfony2 yet, |
| 16 | +follow these steps to do so. |
| 17 | + |
| 18 | +Add the following to ``bin/vendors.sh`` |
| 19 | + |
| 20 | +.. code-block:: bash |
| 21 | +
|
| 22 | + #Doctrine Fixtures install_git doctrine-fixtures |
| 23 | + git://github.com/doctrine/data-fixtures.git |
| 24 | +
|
| 25 | +Update vendors and rebuild the bootstrap file |
| 26 | + |
| 27 | +.. code-block:: bash |
| 28 | +
|
| 29 | + bin/vendors.sh bin/build_bootstrap.php |
| 30 | +
|
| 31 | +As the final step in configuration, you have to register the namespace in |
| 32 | +``app/autoload.php`` |
| 33 | + |
| 34 | +.. code-block:: php |
| 35 | +
|
| 36 | + 'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib', |
| 37 | + 'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib', |
| 38 | +
|
| 39 | +Note that namespaces are registered with preference to the first match. Make |
| 40 | +sure ``Doctrine\Common`` is registered after |
| 41 | +``Doctrine\\Common\\DataFixtures``. |
| 42 | + |
| 43 | +Simple Fixtures |
| 44 | +--------------- |
| 45 | + |
| 46 | +The ideal place to store your fixtures is inside |
| 47 | +``Vendor/MyBundle/DataFixtures/ORM`` and ``Vendor/MyBundle/DataFixtures/ODM`` |
| 48 | +respectively for ORM and ODM. |
| 49 | + |
| 50 | +In this tutorial we will assume you are using ORM. If you are using ODM make |
| 51 | +the changes as required. |
| 52 | + |
| 53 | +In our first fixture we will add a default user to the table of ``User`` |
| 54 | +entity. |
| 55 | + |
| 56 | +.. code-block:: php |
| 57 | +
|
| 58 | + <?php |
| 59 | +
|
| 60 | + //Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php |
| 61 | + namespace Vendor\MyBundle\DataFixtures\ORM; |
| 62 | +
|
| 63 | + use Doctrine\Common\DataFixtures\FixtureInterface; |
| 64 | + use Vendor\MyBundle\Entity\User; //Modify this to use your entity |
| 65 | +
|
| 66 | + class LoadUserData implements FixtureInterface |
| 67 | + { |
| 68 | + public function load($manager) |
| 69 | + { |
| 70 | + $userAdmin = new User(); |
| 71 | + $userAdmin->setUsername('admin'); |
| 72 | + $userAdmin->setPassword('test'); |
| 73 | +
|
| 74 | + $manager->persist($userAdmin); |
| 75 | + $manager->flush() |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | +Writing fixtures this way is quite easy and simple but is not sufficient when |
| 80 | +you are building something serious. The most serious limitation is that you |
| 81 | +can not share objects between fixtures. Lets see how we can overcome this |
| 82 | +limitation in the next section. |
| 83 | + |
| 84 | +Sharing Objects Between Fixtures |
| 85 | +-------------------------------- |
| 86 | + |
| 87 | +.. code-block:: php |
| 88 | +
|
| 89 | + <?php |
| 90 | + |
| 91 | + //Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php |
| 92 | + namespace Vendor\MyBundle\DataFixtures\ORM; |
| 93 | +
|
| 94 | + use Doctrine\Common\DataFixtures\AbstractFixture; |
| 95 | + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
| 96 | + use Vendor\MyBundle\Entity\User; //Modify this to use your entity |
| 97 | +
|
| 98 | + class LoadUserData extends AbstractFixture implements OrderedFixtureInterface |
| 99 | + { |
| 100 | + public function load($manager) |
| 101 | + { |
| 102 | + $userAdmin = new User(); |
| 103 | + $userAdmin->setUsername('admin'); |
| 104 | + $userAdmin->setPassword('test'); |
| 105 | +
|
| 106 | + $manager->persist($userAdmin); |
| 107 | + $manager->flush(); |
| 108 | + |
| 109 | + $this->addReference('admin-user', $userAdmin); |
| 110 | + } |
| 111 | +
|
| 112 | + public function getOrder() |
| 113 | + { |
| 114 | + return 1; // the order in which fixtures will be loaded |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | +
|
| 119 | +.. code-block:: php |
| 120 | +
|
| 121 | + <?php |
| 122 | + |
| 123 | + //Vendor/MyBundle/DataFixtures/ORM/LoadGroupData.php |
| 124 | + namespace Vendor\MyBundle\DataFixtures\ORM; |
| 125 | +
|
| 126 | + use Doctrine\Common\DataFixtures\AbstractFixture; |
| 127 | + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
| 128 | + use Vendor\MyBundle\Entity\Group; //Modify this to use your entity |
| 129 | +
|
| 130 | + class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface |
| 131 | + { |
| 132 | + public function load($manager) |
| 133 | + { |
| 134 | + $groupAdmin = new Group(); |
| 135 | + $groupAdmin->setGroupName('admin'); |
| 136 | +
|
| 137 | + $manager->persist($groupAdmin); |
| 138 | + $manager->flush(); |
| 139 | + |
| 140 | + $this->addReference('admin-group', $groupAdmin); |
| 141 | + } |
| 142 | +
|
| 143 | + public function getOrder() |
| 144 | + { |
| 145 | + return 2; // the order in which fixtures will be loaded |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | +.. code-block:: php |
| 150 | +
|
| 151 | + <?php |
| 152 | + |
| 153 | + //Vendor/MyBundle/DataFixtures/ORM/LoadUserGroupData.php |
| 154 | + namespace Vendor\MyBundle\DataFixtures\ORM; |
| 155 | +
|
| 156 | + use Doctrine\Common\DataFixtures\AbstractFixture; |
| 157 | + use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
| 158 | + use Vendor\MyBundle\Entity\UserGroup; //Modify this to use your entity |
| 159 | + |
| 160 | + class LoadUserGroupData extends AbstractFixture implements OrderedFixtureInterface |
| 161 | + { |
| 162 | + public function load($manager) |
| 163 | + { |
| 164 | + $userGroupAdmin = new UserGroup(); |
| 165 | + $userGroupAdmin->setUser($manager->merge($this->getReference('admin-user'))); |
| 166 | + $userGroupAdmin->setGroup($manager->merge($this->getReference('admin-group'))); |
| 167 | +
|
| 168 | + $manager->persist($userGroupAdmin); |
| 169 | + $manager->flush(); |
| 170 | + } |
| 171 | +
|
| 172 | + public function getOrder() |
| 173 | + { |
| 174 | + return 3; // the order in which fixtures will be loaded |
| 175 | + } |
| 176 | + } |
| 177 | +
|
| 178 | +A brief explanation on how this works. |
| 179 | + |
| 180 | +The fixtures will be executed in the ascending order of the value returned by |
| 181 | +``getOrder()``. Any object that is set with the ``setReference`` method and |
| 182 | +can be accessed via ``getReference`` in fixtures, which are of higher order. |
| 183 | + |
| 184 | +.. _`Doctrine Data Fixtures`: https://github.com/doctrine/data-fixtures |
0 commit comments