Skip to content

Commit e0b83a3

Browse files
committed
Merge pull request symfony#2669 from dbu/couchdb-phpcr-reusable-models
update cookbook for couch and phpcr and give some more hints and background
2 parents a15a9d5 + f79be5e commit e0b83a3

File tree

1 file changed

+85
-12
lines changed

1 file changed

+85
-12
lines changed

cookbook/doctrine/mapping_model_classes.rst

+85-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@ register the mappings for your model classes.
1717
just to get the auto mapping, use the compiler pass.
1818

1919
.. 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+
2232

2333
In your bundle class, write the following code to register the compiler pass::
2434

2535
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
2636
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
37+
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
38+
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
2739

2840
class FOSUserBundle extends Bundle
2941
{
@@ -37,28 +49,89 @@ In your bundle class, write the following code to register the compiler pass::
3749
$modelDir => 'FOS\UserBundle\Model',
3850
);
3951

40-
$ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection'
41-
. '\Compiler\DoctrineOrmMappingsPass';
52+
$ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
4253
if (class_exists($ormCompilerClass)) {
4354
$container->addCompilerPass(
4455
DoctrineOrmMappingsPass::createXmlMappingDriver(
45-
$mappings, 'fos_user.backend_type_orm'
56+
$mappings,
57+
'fos_user.backend_type_orm'
4658
));
4759
}
4860

49-
$mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection'
50-
. '\Compiler\DoctrineMongoDBMappingsPass';
61+
$mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass';
5162
if (class_exists($mongoCompilerClass)) {
5263
$container->addCompilerPass(
5364
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'
5576
));
5677
}
5778

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+
}
5987
}
6088
}
6189

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

Comments
 (0)