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 1 commit
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:`../doctrine/mapping_model_classes` for
Copy link
Member

Choose a reason for hiding this comment

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

use /cookbook/doctrine/mapping_model.classes

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
40 changes: 40 additions & 0 deletions cookbook/doctrine/mapping_model_classes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.. 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.


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

class FOSUserBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ValidationPass());

if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
$mappings = array(
realpath(__DIR__.'/Resources/config/doctrine/model') => 'FOS\UserBundle\Model',
);
$container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, 'fos_user.backend_type_orm'));
Copy link
Member

Choose a reason for hiding this comment

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

please at some line breaking in this example to avoid horizontal scrollbars (max. length of 85 characters)

}

// TODO: couch, mongo
}
}

The compiler pass provides factory methods for all drivers: Annotations, XML, Yaml, PHP and StaticPHP.
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