Skip to content

Commit c507987

Browse files
committed
[cookbook][fixtures] Adding more to the fixture cookbook entry
1 parent d0da548 commit c507987

File tree

1 file changed

+88
-36
lines changed

1 file changed

+88
-36
lines changed

cookbook/doctrine/doctrine_fixtures.rst

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,75 @@
11
.. index::
22
single: Doctrine; Creating fixtures in Symfony2
33

4-
How to create fixtures in Symfony2
4+
How to create Fixtures in Symfony2
55
==================================
66

77
Fixtures are used to load the database with a set of data. This data can
88
either be for testing or could be the initial data required for the
99
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.
10+
but Doctrine2 has a library to help you write fixtures for the Doctrine
11+
:doc:`ORM</book/doctrine/orm/overview>` or :doc:`ODM</book/doctrine/mongodb-odm/overview>`.
1112

1213
Setup and Configuration
1314
-----------------------
1415

15-
If you don't have `Doctrine Data Fixtures`_ configured with Symfony2 yet,
16-
follow these steps to do so.
16+
If you don't have the `Doctrine Data Fixtures`_ library configured with Symfony2
17+
yet, follow these steps to do so.
1718

18-
Add the following to ``bin/vendors.sh``
19+
Add the following to ``bin/vendors.sh``, right after the "Monolog" entry:
1920

20-
.. code-block:: bash
21+
.. code-block:: text
2122
22-
#Doctrine Fixtures install_git doctrine-fixtures
23-
git://github.com/doctrine/data-fixtures.git
23+
# Doctrine Fixtures
24+
install_git doctrine-fixtures git://github.com/doctrine/data-fixtures.git
2425
25-
Update vendors and rebuild the bootstrap file
26+
Update vendors and rebuild the bootstrap file:
2627

2728
.. code-block:: bash
2829
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``
30+
$ bin/vendors.sh bin/build_bootstrap.php
3331
34-
.. code-block:: php
32+
If everything worked, the ``doctrine-fixtures`` library can now be found
33+
at ``vendor/doctrine-fixtures``.
3534

36-
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
37-
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
35+
Finally, register the ``Doctrine\Common\DataFixtures`` namespace in ``app/autoload.php``.
3836

39-
Note that namespaces are registered with preference to the first match. Make
40-
sure ``Doctrine\Common`` is registered after
41-
``Doctrine\\Common\\DataFixtures``.
37+
.. code-block:: php
4238
43-
Simple Fixtures
44-
---------------
39+
// ...
40+
$loader->registerNamespaces(array(
41+
// ...
42+
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
43+
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
44+
// ...
45+
));
46+
47+
Be sure to register the new namespace *after* ``Doctrine\Common``. Otherwise,
48+
Symfony will look for data fixture classes inside the ``Doctrine\Common``
49+
directory. Symfony's autoloader always looks for a class inside the directory
50+
of the first matching namespace, so more specific namespaces should always
51+
come first.
52+
53+
Writing Simple Fixtures
54+
-----------------------
4555

4656
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.
57+
``src/VendorName/MyBundle/DataFixtures/ORM`` and ``src/VendorName/MyBundle/DataFixtures/ODM``
58+
respectively for the ORM and ODM. This tutorial assumes that you are using
59+
the ORM - but fixtures can be added just as easily if you're using the ODM.
5260

53-
In our first fixture we will add a default user to the table of ``User``
54-
entity.
61+
Imagine that you have a ``User`` class, and you'd like to load one ``User``
62+
entry:
5563

5664
.. code-block:: php
5765
5866
<?php
5967
60-
//Vendor/MyBundle/DataFixtures/ORM/LoadUserData.php
61-
namespace Vendor\MyBundle\DataFixtures\ORM;
68+
// src/VendorName/MyBundle/DataFixtures/ORM/LoadUserData.php
69+
namespace VendorName\MyBundle\DataFixtures\ORM;
6270
6371
use Doctrine\Common\DataFixtures\FixtureInterface;
64-
use Vendor\MyBundle\Entity\User; //Modify this to use your entity
72+
use VendorName\MyBundle\Entity\User;
6573
6674
class LoadUserData implements FixtureInterface
6775
{
@@ -76,12 +84,56 @@ entity.
7684
}
7785
}
7886
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.
87+
In Doctrine2, fixtures are just objects where you load data by interacting
88+
with your entities as you normal do. This allows you to create the exact
89+
fixtures you want for your application.
90+
91+
The most serious limitation is that you can not share objects between fixtures.
92+
Later, you'll see how to overcome this limitation.
93+
94+
Executing Fixtures
95+
------------------
96+
97+
Once your fixtures have been written, you load the fixtures via the command
98+
line via the ``doctrine:data:load`` command:
99+
100+
.. code-block:: bash
101+
102+
$ php app/console doctrine:data:load
103+
104+
If you're using the ODM, use the ``doctrine:mongodb:data:load`` command instead:
105+
106+
.. code-block:: bash
107+
108+
$ php app/console doctrine:mongodb:data:load
109+
110+
The task will look inside the ``DataFixtures/ORM`` (or ``DataFixtures/ODM``
111+
for the ODM) directory of each bundle and execute each class that implements
112+
the ``FixtureInterface``.
113+
114+
Both commands come with a few options:
115+
116+
* ``--fixtures=/path/to/fixture`` - Use this option to manually specify the
117+
directory or file where the fixtures classes should be loaded;
118+
119+
* ``--append`` - Use this flag to append data instead of deleting data before
120+
loading it (the default behavior);
121+
122+
* ``--em=manager_name`` - Manually specify the entity manager to use for
123+
loading the data.
124+
125+
.. note::
126+
127+
If using the ``doctrine:mongodb:data:load`` task, replace the ``--em=``
128+
option with ``--dm=`` to manually specify the document manager.
129+
130+
A full example use might look like:
131+
132+
.. code-block:: bash
133+
134+
$ php app/console doctrine:data:load --fixtures=/path/to/fixture1 --fixtures=/path/to/fixture2 --append --em=foo_manager
83135
84-
Sharing Objects Between Fixtures
136+
Sharing Objects between Fixtures
85137
--------------------------------
86138

87139
.. code-block:: php

0 commit comments

Comments
 (0)