1
1
.. index ::
2
2
single: Doctrine; Creating fixtures in Symfony2
3
3
4
- How to create fixtures in Symfony2
4
+ How to create Fixtures in Symfony2
5
5
==================================
6
6
7
7
Fixtures are used to load the database with a set of data. This data can
8
8
either be for testing or could be the initial data required for the
9
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.
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> `.
11
12
12
13
Setup and Configuration
13
14
-----------------------
14
15
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.
17
18
18
- Add the following to ``bin/vendors.sh ``
19
+ Add the following to ``bin/vendors.sh ``, right after the "Monolog" entry:
19
20
20
- .. code-block :: bash
21
+ .. code-block :: text
21
22
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
24
25
25
- Update vendors and rebuild the bootstrap file
26
+ Update vendors and rebuild the bootstrap file:
26
27
27
28
.. code-block :: bash
28
29
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
33
31
34
- .. code-block :: php
32
+ If everything worked, the ``doctrine-fixtures `` library can now be found
33
+ at ``vendor/doctrine-fixtures ``.
35
34
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 ``.
38
36
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
42
38
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
+ -----------------------
45
55
46
56
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.
52
60
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:
55
63
56
64
.. code-block :: php
57
65
58
66
<?php
59
67
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;
62
70
63
71
use Doctrine\Common\DataFixtures\FixtureInterface;
64
- use Vendor \MyBundle\Entity\User; //Modify this to use your entity
72
+ use VendorName \MyBundle\Entity\User;
65
73
66
74
class LoadUserData implements FixtureInterface
67
75
{
@@ -76,12 +84,56 @@ entity.
76
84
}
77
85
}
78
86
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
83
135
84
- Sharing Objects Between Fixtures
136
+ Sharing Objects between Fixtures
85
137
--------------------------------
86
138
87
139
.. code-block :: php
0 commit comments