Skip to content

Commit 79cb4f1

Browse files
committed
feature symfony#4069 document the namespace alias feature (dbu)
This PR was merged into the master branch. Discussion ---------- document the namespace alias feature | Q | A | ------------- | --- | Doc fix? | no | New docs? | [yes](symfony/symfony#10853) | Applies to | 2.6 | Fixed tickets | - Commits ------- d753d0e document the namespace alias feature
2 parents c53d9d6 + d753d0e commit 79cb4f1

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

cookbook/doctrine/mapping_model_classes.rst

+35-26
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ register the mappings for your model classes.
1414
For non-reusable bundles, the easiest option is to put your model classes
1515
in the default locations: ``Entity`` for the Doctrine ORM or ``Document``
1616
for one of the ODMs. For reusable bundles, rather than duplicate model classes
17-
just to get the auto mapping, use the compiler pass.
17+
just to get the auto-mapping, use the compiler pass.
1818

1919
.. versionadded:: 2.3
2020
The base mapping compiler pass was introduced in Symfony 2.3. The Doctrine bundles
2121
support it from DoctrineBundle >= 1.2.1, MongoDBBundle >= 3.0.0,
22-
PHPCRBundle >= 1.0.0-alpha2 and the (unversioned) CouchDBBundle supports the
22+
PHPCRBundle >= 1.0.0 and the (unversioned) CouchDBBundle supports the
2323
compiler pass since the `CouchDB Mapping Compiler Pass pull request`_
2424
was merged.
2525

26-
If you want your bundle to support older versions of Symfony and
27-
Doctrine, you can provide a copy of the compiler pass in your bundle.
28-
See for example the `FOSUserBundle mapping configuration`_
29-
``addRegisterMappingsPass``.
30-
26+
.. versionadded:: 2.6
27+
Support for defining namespace aliases was introduced in Symfony 2.6.
28+
It is safe to define the aliases with older versions of Symfony as
29+
the aliases are the last argument to ``createXmlMappingDriver`` and
30+
are ignored by PHP if that argument doesn't exist.
3131

3232
In your bundle class, write the following code to register the compiler pass.
33-
This one is written for the FOSUserBundle, so parts of it will need to
33+
This one is written for the CmfRoutingBundle, so parts of it will need to
3434
be adapted for your case::
3535

3636
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
3737
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
3838
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
3939
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
4040

41-
class FOSUserBundle extends Bundle
41+
class CmfRoutingBundle extends Bundle
4242
{
4343
public function build(ContainerBuilder $container)
4444
{
@@ -47,16 +47,17 @@ be adapted for your case::
4747

4848
$modelDir = realpath(__DIR__.'/Resources/config/doctrine/model');
4949
$mappings = array(
50-
$modelDir => 'FOS\UserBundle\Model',
50+
$modelDir => 'Symfony\Cmf\RoutingBundle\Model',
5151
);
5252

5353
$ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
5454
if (class_exists($ormCompilerClass)) {
5555
$container->addCompilerPass(
5656
DoctrineOrmMappingsPass::createXmlMappingDriver(
5757
$mappings,
58-
array('fos_user.model_manager_name'),
59-
'fos_user.backend_type_orm'
58+
array('cmf_routing.model_manager_name'),
59+
'cmf_routing.backend_type_orm',
60+
array('CmfRoutingBundle' => 'Symfony\Cmf\RoutingBundle\Model')
6061
));
6162
}
6263

@@ -65,8 +66,9 @@ be adapted for your case::
6566
$container->addCompilerPass(
6667
DoctrineMongoDBMappingsPass::createXmlMappingDriver(
6768
$mappings,
68-
array('fos_user.model_manager_name'),
69-
'fos_user.backend_type_mongodb'
69+
array('cmf_routing.model_manager_name'),
70+
'cmf_routing.backend_type_mongodb',
71+
array('CmfRoutingBundle' => 'Symfony\Cmf\RoutingBundle\Model')
7072
));
7173
}
7274

@@ -75,8 +77,9 @@ be adapted for your case::
7577
$container->addCompilerPass(
7678
DoctrineCouchDBMappingsPass::createXmlMappingDriver(
7779
$mappings,
78-
array('fos_user.model_manager_name'),
79-
'fos_user.backend_type_couchdb'
80+
array('cmf_routing.model_manager_name'),
81+
'cmf_routing.backend_type_couchdb',
82+
array('CmfRoutingBundle' => 'Symfony\Cmf\RoutingBundle\Model')
8083
));
8184
}
8285

@@ -85,8 +88,9 @@ be adapted for your case::
8588
$container->addCompilerPass(
8689
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
8790
$mappings,
88-
array('fos_user.model_manager_name'),
89-
'fos_user.backend_type_phpcr'
91+
array('cmf_routing.model_manager_name'),
92+
'cmf_routing.backend_type_phpcr',
93+
array('CmfRoutingBundle' => 'Symfony\Cmf\RoutingBundle\Model')
9094
));
9195
}
9296
}
@@ -99,17 +103,20 @@ decide which to use.
99103
The compiler pass provides factory methods for all drivers provided by Doctrine:
100104
Annotations, XML, Yaml, PHP and StaticPHP. The arguments are:
101105

102-
* a map/hash of absolute directory path to namespace;
103-
* an array of container parameters that your bundle uses to specify the name of
104-
the Doctrine manager that it is using. In the above example, the FOSUserBundle
105-
stores the manager name that's being used under the ``fos_user.model_manager_name``
106+
* A map/hash of absolute directory path to namespace;
107+
* An array of container parameters that your bundle uses to specify the name of
108+
the Doctrine manager that it is using. In the example above, the CmfRoutingBundle
109+
stores the manager name that's being used under the ``cmf_routing.model_manager_name``
106110
parameter. The compiler pass will append the parameter Doctrine is using
107111
to specify the name of the default manager. The first parameter found is
108112
used and the mappings are registered with that manager;
109-
* an optional container parameter name that will be used by the compiler
113+
* An optional container parameter name that will be used by the compiler
110114
pass to determine if this Doctrine type is used at all. This is relevant if
111115
your user has more than one type of Doctrine bundle installed, but your
112-
bundle is only used with one type of Doctrine.
116+
bundle is only used with one type of Doctrine;
117+
* A map/hash of aliases to namespace. This should be the same convention used
118+
by Doctrine auto-mapping. In the example above, this allows the user to call
119+
``$om->getRepository('CmfRoutingBundle:Route')``.
113120

114121
.. note::
115122

@@ -120,7 +127,7 @@ Annotations, XML, Yaml, PHP and StaticPHP. The arguments are:
120127
of the class as their filename (e.g. ``BlogPost.orm.xml``)
121128

122129
If you also need to map a base class, you can register a compiler pass
123-
with the ``DefaultFileLocator`` like this. This code is simply taken from the
130+
with the ``DefaultFileLocator`` like this. This code is taken from the
124131
``DoctrineOrmMappingsPass`` and adapted to use the ``DefaultFileLocator``
125132
instead of the ``SymfonyFileLocator``::
126133

@@ -138,6 +145,9 @@ Annotations, XML, Yaml, PHP and StaticPHP. The arguments are:
138145
);
139146
}
140147

148+
Note that you do not need to provide a namespace alias unless your users are
149+
expected to ask Doctrine for the base classes.
150+
141151
Now place your mapping file into ``/Resources/config/doctrine-base`` with the
142152
fully qualified class name, separated by ``.`` instead of ``\``, for example
143153
``Other.Namespace.Model.Name.orm.xml``. You may not mix the two as otherwise
@@ -146,4 +156,3 @@ Annotations, XML, Yaml, PHP and StaticPHP. The arguments are:
146156
Adjust accordingly for the other Doctrine implementations.
147157

148158
.. _`CouchDB Mapping Compiler Pass pull request`: https://github.com/doctrine/DoctrineCouchDBBundle/pull/27
149-
.. _`FOSUserBundle mapping configuration`: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserBundle.php

0 commit comments

Comments
 (0)