5
5
Configuration
6
6
=============
7
7
8
+ In the overview we already described the only necessary configuration option
9
+ "mappings" to get the Doctrine ORM running with Symfony 2. All the other
10
+ configuration options are used with reasonable default values.
11
+
12
+ This following configuration example shows all the configuration defaults that
13
+ the ORM resolves to:
14
+
15
+ .. code-block :: yaml
16
+
17
+ doctrine.orm :
18
+ mappings :
19
+ HelloBundle : ~
20
+ auto_generate_proxy_classes : true
21
+ proxy_namespace : Proxies
22
+ proxy_dir : %kernel.cache_dir%/doctrine/orm/Proxies
23
+ default_entity_manager : default
24
+ default_connection : default
25
+ metadata_cache_driver : array
26
+ query_cache_driver : array
27
+ result_cache_driver : array
28
+
29
+ There are lots of other configuration options that you can use to overwrite
30
+ certain classes, but those are for very advanced use-cases only. You should
31
+ look at the "orm.xml" file in the DoctrineBundle to get an overview of all the
32
+ supported options.
33
+
34
+ For the caching drivers you can specifiy the values "array", "apc", "memcache"
35
+ or "xcache".
36
+
37
+ The following example shows an overview of the caching configurations:
38
+
39
+ .. code-block :: yaml
40
+
41
+ doctrine.orm :
42
+ mappings :
43
+ HelloBundle : ~
44
+ metadata_cache_driver : apc
45
+ query_cache_driver : xcache
46
+ result_cache_driver :
47
+ type : memcache
48
+ host : localhost
49
+ port : 11211
50
+ instance_class : Memcache
51
+
52
+ Mapping Configuration
53
+ ~~~~~~~~~~~~~~~~~~~~~
54
+
55
+ Explicit definition of all the mapped entities is the only necessary
56
+ configuration for the ORM and there are several configuration options that you
57
+ can control. The following configuration options exist for a mapping:
58
+
59
+ - ``type `` One of "annotations", "xml", "yml", "php" or "static-php". This
60
+ specifies which type of metadata type your mapping uses.
61
+ - ``dir `` Path to the mapping or entity files (depending on the driver). If
62
+ this path is relative it is assumed to be relative to the bundle root. This
63
+ only works if the name of your mapping is a bundle name. If you want to use
64
+ this option to specifiy absolute paths you should prefix the path with the
65
+ kernel parameters that exist in the DIC (for example %kernel.dir%).
66
+ - ``prefix `` A common namespace prefix that all entities of this mapping
67
+ share. This prefix should never conflict with prefixes of other defined
68
+ mappings otherwise some of your entities cannot be found by Doctrine. This
69
+ option defaults to the bundle namespace + `Entities `, for example for an
70
+ application bundle called "Hello" prefix would be
71
+ "Application\H ello\E ntities".
72
+ - ``alias `` Doctrine offers a way to alias entity namespaces to simpler,
73
+ shorter names to be used in DQL queries or for Repository access.
74
+ - ``is_bundle `` This option is a derived value from ``dir `` and by default is
75
+ set to true if dir is relative proved by a ``file_exists() `` check that
76
+ returns false. It is false if the existance check returns true. In this case
77
+ an absolute path was specified and the metadata files are most likely in a
78
+ directory outside of a bundle.
79
+
80
+ To avoid having to configure lots of information for your mappings you should
81
+ follow these conventions:
82
+
83
+ 1. Put all your entities in a directory Entities/ inside your bundle. For
84
+ example "Application/Hello/Entities/".
85
+ 2. If you are using xml, yml or php mapping put all your configuration files
86
+ into the "Resources/config/doctrine/metadata/doctrine/orm/" directory sufficed
87
+ with dcm.xml, dcm.yml or dcm.php respectively.
88
+ 3. Annotations is assumed if an "Entities/" but no
89
+ "Resources/config/doctrine/metadata/doctrine/orm/" directory is found.
90
+
91
+ The following configuration shows a bunch of mapping examples:
92
+
93
+ .. code-block :: yaml
94
+
95
+ doctrine.orm :
96
+ mappings :
97
+ MyBundle1 : ~
98
+ MyBundle2 : yml
99
+ MyBundle3 : { type: annotation, dir: Entities/ }
100
+ MyBundle4 : { type: xml, dir: Resources/config/doctrine/mapping }
101
+ MyBundle5 :
102
+ type : yml
103
+ dir : my-bundle-mappings-dir
104
+ alias : BundleAlias
105
+ doctrine_extensions :
106
+ type : xml
107
+ dir : %kernel.dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Entities
108
+ prefix : DoctrineExtensions\Entities\
109
+ alias : DExt
110
+
111
+ Multiple Entity Managers
112
+ ~~~~~~~~~~~~~~~~~~~~~~~~
113
+
114
+ You can use multiple EntityManagers in a Symfony application. This is
115
+ necessary if you are using different databases or even vendors with entirely
116
+ different sets of entities.
117
+
118
+ The following configuration code shows how to define two EntityManagers:
119
+
8
120
.. code-block :: yaml
9
121
10
122
doctrine.orm :
@@ -16,38 +128,22 @@ Configuration
16
128
customer :
17
129
connection : customer
18
130
19
- Just like the DBAL, if you have configured multiple ``EntityManager `` instances
20
- and want to get a specific one you can use the `` getEntityManager() `` method by
21
- just passing it an argument that is the `` EntityManager `` name you want ::
131
+ Just like the DBAL, if you have configured multiple ``EntityManager ``
132
+ instances and want to get a specific one you can use the full service name to
133
+ retrieve it from the Symfony Dependency Injection Container ::
22
134
23
135
class UserController extends Controller
24
136
{
25
137
public function indexAction()
26
138
{
27
- $em = $this->get('doctrine.orm.customer_entity_manager');
28
- }
29
- }
30
-
31
- Now the scenario arrises where you want to change your mapping information and
32
- update your development database schema without blowing away everything and
33
- losing your existing data. So first lets just add a new property to our ``User ``
34
- entity::
35
-
36
- namespace Application\HelloBundle\Entities;
37
-
38
- /** @orm:Entity */
39
- class User
40
- {
41
- /** @orm:Column(type="string") */
42
- protected $new;
139
+ $em = $this->get('doctrine.orm.entity_manager');
140
+ $defaultEm = $this->get('doctrine.orm.default_entity_manager');
141
+ $customerEm = $this->get('doctrine.orm.customer_entity_manager');
43
142
44
- // ...
143
+ // $em === $defaultEm => true
144
+ // $defaultEm === $customerEm => false
145
+ }
45
146
}
46
147
47
- Once you've done that, to get your database schema updated with the new column
48
- you just need to run the following command:
49
-
50
- $ php app/console doctrine:schema: update
51
-
52
- Now your database will be updated and the new column added to the database
53
- table.
148
+ The service "doctrine.orm.entity_manager" is an alias for the default entity
149
+ manager defined in the "default_entity_manager" configuration option.
0 commit comments