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