@@ -17,13 +17,25 @@ register the mappings for your model classes.
17
17
just to get the auto mapping, use the compiler pass.
18
18
19
19
.. versionadded :: 2.3
20
- The base mapping compiler pass was added in Symfony 2.3. The doctrine bundles
21
- support it from DoctrineBundle >= 1.2.1, MongoDBBundle >= 3.0.0
20
+
21
+ The base mapping compiler pass was added in Symfony 2.3. The Doctrine bundles
22
+ support it from DoctrineBundle >= 1.2.1, MongoDBBundle >= 3.0.0,
23
+ PHPCRBundle >= 1.0.0-alpha2 and the (unversioned) CouchDBBundle supports the
24
+ compiler pass since the `CouchDB Mapping Compiler Pass pull request `_
25
+ was merged.
26
+
27
+ If you want your bundle to support older versions of Symfony and
28
+ Doctrine, you can provide a copy of the compiler pass in your bundle.
29
+ See for example the `FOSUserBundle mapping configuration `_
30
+ ``addRegisterMappingsPass ``.
31
+
22
32
23
33
In your bundle class, write the following code to register the compiler pass::
24
34
25
35
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
26
36
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
37
+ use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
38
+ use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
27
39
28
40
class FOSUserBundle extends Bundle
29
41
{
@@ -37,28 +49,89 @@ In your bundle class, write the following code to register the compiler pass::
37
49
$modelDir => 'FOS\UserBundle\Model',
38
50
);
39
51
40
- $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection'
41
- . '\Compiler\DoctrineOrmMappingsPass';
52
+ $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
42
53
if (class_exists($ormCompilerClass)) {
43
54
$container->addCompilerPass(
44
55
DoctrineOrmMappingsPass::createXmlMappingDriver(
45
- $mappings, 'fos_user.backend_type_orm'
56
+ $mappings,
57
+ 'fos_user.backend_type_orm'
46
58
));
47
59
}
48
60
49
- $mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection'
50
- . '\Compiler\DoctrineMongoDBMappingsPass';
61
+ $mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass';
51
62
if (class_exists($mongoCompilerClass)) {
52
63
$container->addCompilerPass(
53
64
DoctrineMongoDBMappingsPass::createXmlMappingDriver(
54
- $mappings, 'fos_user.backend_type_mongodb'
65
+ $mappings,
66
+ 'fos_user.backend_type_mongodb'
67
+ ));
68
+ }
69
+
70
+ $couchCompilerClass = 'Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass';
71
+ if (class_exists($couchCompilerClass)) {
72
+ $container->addCompilerPass(
73
+ DoctrineCouchDBMappingsPass::createXmlMappingDriver(
74
+ $mappings,
75
+ 'fos_user.backend_type_couchdb'
55
76
));
56
77
}
57
78
58
- // TODO: couch
79
+ $phpcrCompilerClass = 'Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass';
80
+ if (class_exists($phpcrCompilerClass)) {
81
+ $container->addCompilerPass(
82
+ DoctrinePhpcrMappingsPass::createXmlMappingDriver(
83
+ $mappings,
84
+ 'fos_user.backend_type_phpcr'
85
+ ));
86
+ }
59
87
}
60
88
}
61
89
62
- The compiler pass provides factory methods for all drivers provided by the
63
- bundle: Annotations, XML, Yaml, PHP and StaticPHP for Doctrine ORM, the ODM
64
- bundles sometimes do not have all of those drivers.
90
+ Note the :phpfunction: `class_exists ` check. This is crucial, as you do not want your
91
+ bundle to have a hard dependency on all Doctrine bundles but let the user
92
+ decide which to use.
93
+
94
+ The compiler pass provides factory methods for all drivers provided by Doctrine:
95
+ Annotations, XML, Yaml, PHP and StaticPHP. The arguments are:
96
+
97
+ * a map of absolute directory path to namespace;
98
+ * an array of container parameters that your bundle uses to specify the name of
99
+ the Doctrine manager that it is using. The compiler pass will append the
100
+ parameter Doctrine is using to specify the name of the default manager. The
101
+ first parameter found is used and the mappings are registered with that
102
+ manager;
103
+ * an optional container parameter name that will be used by the compiler
104
+ pass to determine if this Doctrine type is used at all (this is relevant if
105
+ your user has more than one type of Doctrine bundle installed, but your
106
+ bundle is only used with one type of Doctrine.
107
+
108
+ .. note ::
109
+
110
+ The factory method is using the ``SymfonyFileLocator `` of Doctrine, meaning
111
+ it will only see XML and YML mapping files if they do not contain the
112
+ namespace. If you also need to map a base class, you can register a
113
+ compiler pass with the ``DefaultFileLocator `` like this::
114
+
115
+ private function buildMappingCompilerPass()
116
+ {
117
+ $arguments = array(array(realpath(__DIR__ . '/Resources/config/doctrine-base')), '.orm.xml');
118
+ $locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator', $arguments);
119
+ $driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', array($locator));
120
+
121
+ return new DoctrineOrmMappingsPass(
122
+ $driver,
123
+ array('Full\Namespace'),
124
+ array('your_bundle.manager_name'),
125
+ 'your_bundle.orm_enabled'
126
+ );
127
+ }
128
+
129
+ And place your mapping file into ``/Resources/config/doctrine-base `` with the
130
+ fully qualified class name, separated by ``. `` instead of ``\ ``, for example
131
+ ``Other.Namespace.Model.Name.orm.xml ``. You may not mix the two as otherwise
132
+ the SymfonyFileLocator will get confused.
133
+
134
+ Adjust accordingly for the other Doctrine implementations.
135
+
136
+ .. _`CouchDB Mapping Compiler Pass pull request` : https://github.com/doctrine/DoctrineCouchDBBundle/pull/27
137
+ .. _`FOSUserBundle mapping configuration` : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserBundle.php
0 commit comments