From c28c3ae427d7fc5d6b2fa790f1d2c4c1f7cf1192 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 15 Apr 2013 21:10:17 +0200 Subject: [PATCH 1/3] document the doctrine compiler pass --- cookbook/bundles/best_practices.rst | 6 ++++ cookbook/doctrine/index.rst | 1 + cookbook/doctrine/mapping_model_classes.rst | 40 +++++++++++++++++++++ cookbook/map.rst.inc | 1 + 4 files changed, 48 insertions(+) create mode 100644 cookbook/doctrine/mapping_model_classes.rst diff --git a/cookbook/bundles/best_practices.rst b/cookbook/bundles/best_practices.rst index 337e802b475..2d1e6ed39a8 100644 --- a/cookbook/bundles/best_practices.rst +++ b/cookbook/bundles/best_practices.rst @@ -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 + how to handle the mapping with a compiler pass. + Classes ------- diff --git a/cookbook/doctrine/index.rst b/cookbook/doctrine/index.rst index 170a5f2d718..7f19ef7d4fe 100644 --- a/cookbook/doctrine/index.rst +++ b/cookbook/doctrine/index.rst @@ -12,4 +12,5 @@ Doctrine multiple_entity_managers custom_dql_functions resolve_target_entity + mapping_model_classes registration_form diff --git a/cookbook/doctrine/mapping_model_classes.rst b/cookbook/doctrine/mapping_model_classes.rst new file mode 100644 index 00000000000..f1ef1b641fd --- /dev/null +++ b/cookbook/doctrine/mapping_model_classes.rst @@ -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')); + } + + // TODO: couch, mongo + } + } + +The compiler pass provides factory methods for all drivers: Annotations, XML, Yaml, PHP and StaticPHP. diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index ba129af6c37..3053c89229b 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -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` From 2b235f931530cf63b6f83a310ab7561121266ce1 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Tue, 16 Apr 2013 08:21:10 +0200 Subject: [PATCH 2/3] cs cleanup --- cookbook/bundles/best_practices.rst | 2 +- cookbook/doctrine/mapping_model_classes.rst | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cookbook/bundles/best_practices.rst b/cookbook/bundles/best_practices.rst index 2d1e6ed39a8..319b6289504 100644 --- a/cookbook/bundles/best_practices.rst +++ b/cookbook/bundles/best_practices.rst @@ -137,7 +137,7 @@ The following classes and files have specific emplacements: .. note:: When building a reusable bundle, model classes should be placed in the - ``Model`` namespace. See doc:`../doctrine/mapping_model_classes` for + ``Model`` namespace. See :doc:`/cookbook/doctrine/mapping_model_classes` for how to handle the mapping with a compiler pass. Classes diff --git a/cookbook/doctrine/mapping_model_classes.rst b/cookbook/doctrine/mapping_model_classes.rst index f1ef1b641fd..7ba056bba4f 100644 --- a/cookbook/doctrine/mapping_model_classes.rst +++ b/cookbook/doctrine/mapping_model_classes.rst @@ -16,9 +16,14 @@ register the mappings for your model classes. 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 mapping compiler pass was added in Symfony 2.3 and DoctrineBundle 1.2.1. + In your bundle class, write the following code to register the compiler pass:: + use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; + class FOSUserBundle extends Bundle { public function build(ContainerBuilder $container) @@ -26,11 +31,17 @@ In your bundle class, write the following code to register the compiler pass:: parent::build($container); $container->addCompilerPass(new ValidationPass()); - if (class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) { + $compilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection' + . '\Compiler\DoctrineOrmMappingsPass'; + if (class_exists($compilerClass)) { + $modelDir = realpath(__DIR__.'/Resources/config/doctrine/model'); $mappings = array( - realpath(__DIR__.'/Resources/config/doctrine/model') => 'FOS\UserBundle\Model', + $modelDir => 'FOS\UserBundle\Model', ); - $container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, 'fos_user.backend_type_orm')); + $container->addCompilerPass( + DoctrineOrmMappingsPass::createXmlMappingDriver( + $mappings, 'fos_user.backend_type_orm' + )); } // TODO: couch, mongo From 794fc5dfc439ad13b1b82a2e3f9afc3298858dff Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 17 Apr 2013 23:27:11 +0200 Subject: [PATCH 3/3] adding doc for mongodb --- cookbook/doctrine/mapping_model_classes.rst | 34 +++++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/cookbook/doctrine/mapping_model_classes.rst b/cookbook/doctrine/mapping_model_classes.rst index 7ba056bba4f..37bdd45519b 100644 --- a/cookbook/doctrine/mapping_model_classes.rst +++ b/cookbook/doctrine/mapping_model_classes.rst @@ -17,35 +17,49 @@ register the mappings for your model classes. just to get the auto mapping, use the compiler pass. .. versionadded:: 2.3 - The mapping compiler pass was added in Symfony 2.3 and DoctrineBundle 1.2.1. + 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; + use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass; class FOSUserBundle extends Bundle { public function build(ContainerBuilder $container) { parent::build($container); - $container->addCompilerPass(new ValidationPass()); + // ... - $compilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection' + $modelDir = realpath(__DIR__.'/Resources/config/doctrine/model'); + $mappings = array( + $modelDir => 'FOS\UserBundle\Model', + ); + + $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection' . '\Compiler\DoctrineOrmMappingsPass'; - if (class_exists($compilerClass)) { - $modelDir = realpath(__DIR__.'/Resources/config/doctrine/model'); - $mappings = array( - $modelDir => 'FOS\UserBundle\Model', - ); + if (class_exists($ormCompilerClass)) { $container->addCompilerPass( DoctrineOrmMappingsPass::createXmlMappingDriver( $mappings, 'fos_user.backend_type_orm' )); } - // TODO: couch, mongo + $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: Annotations, XML, Yaml, PHP and StaticPHP. +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.