Skip to content

[WCM] document the doctrine compiler pass #2507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cookbook/bundles/best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ The following classes and files have specific emplacements:
| Unit and Functional Tests | ``Tests/`` |
+------------------------------+-----------------------------+

.. note::

When building a reusable bundle, model classes should be placed in the
``Model`` namespace. See :doc:`/cookbook/doctrine/mapping_model_classes` for
how to handle the mapping with a compiler pass.

Classes
-------

Expand Down
1 change: 1 addition & 0 deletions cookbook/doctrine/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Doctrine
multiple_entity_managers
custom_dql_functions
resolve_target_entity
mapping_model_classes
registration_form
65 changes: 65 additions & 0 deletions cookbook/doctrine/mapping_model_classes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. index::
single: Doctrine; Mapping Model classes

How to provide model classes for several Doctrine implementations
=================================================================

When building a bundle that could be used not only with Doctrine ORM but
also the CouchDB ODM, MongoDB ODM or PHPCR ODM, you should still only
write one model class. The Doctrine bundles provide a compiler pass to
register the mappings for your model classes.

.. note::

For non-reusable bundles, the easiest is to put your model classes in
the default locations. ``Entity`` for Doctrine ORM, ``Document`` for one
of the ODMs. For reusable bundles, rather than duplicate model classes
just to get the auto mapping, use the compiler pass.

.. versionadded:: 2.3
The base mapping compiler pass was added in Symfony 2.3, the doctrine bundles
support it from DoctrineBundle >= 1.2.1, MongoDBBundle >= 3.0.0


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

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still a bit too long. should i break it as well? and if so, how?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbu no, namespaces (and CLI commands) are exceptions on this 85 characters rule. They can't be wrapped.

use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;

class FOSUserBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
// ...

$modelDir = realpath(__DIR__.'/Resources/config/doctrine/model');
$mappings = array(
$modelDir => 'FOS\UserBundle\Model',
);

$ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection'
. '\Compiler\DoctrineOrmMappingsPass';
if (class_exists($ormCompilerClass)) {
$container->addCompilerPass(
DoctrineOrmMappingsPass::createXmlMappingDriver(
$mappings, 'fos_user.backend_type_orm'
));
}

$mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection'
. '\Compiler\DoctrineMongoDBMappingsPass';
if (class_exists($mongoCompilerClass)) {
$container->addCompilerPass(
DoctrineMongoDBMappingsPass::createXmlMappingDriver(
$mappings, 'fos_user.backend_type_mongodb'
));
}

// TODO: couch
}
}

The compiler pass provides factory methods for all drivers provided by the
bundle: Annotations, XML, Yaml, PHP and StaticPHP for Doctrine ORM, the ODM
bundles sometimes do not have all of those drivers.
1 change: 1 addition & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* :doc:`/cookbook/doctrine/multiple_entity_managers`
* :doc:`/cookbook/doctrine/custom_dql_functions`
* :doc:`/cookbook/doctrine/resolve_target_entity`
* :doc:`/cookbook/doctrine/mapping_model_classes`
* :doc:`/cookbook/doctrine/registration_form`

* :doc:`/cookbook/email/index`
Expand Down