From f79be5e23a0fc2c13a106bca20cf16669198fc57 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 27 May 2013 16:56:09 +0200 Subject: [PATCH] update cookbook for couch and phpcr and give some more hints and background, fix cs and some typos --- cookbook/doctrine/mapping_model_classes.rst | 97 ++++++++++++++++++--- 1 file changed, 85 insertions(+), 12 deletions(-) diff --git a/cookbook/doctrine/mapping_model_classes.rst b/cookbook/doctrine/mapping_model_classes.rst index 48529cc2e26..0b13eac7500 100644 --- a/cookbook/doctrine/mapping_model_classes.rst +++ b/cookbook/doctrine/mapping_model_classes.rst @@ -17,13 +17,25 @@ register the mappings for your 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 + + 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, + PHPCRBundle >= 1.0.0-alpha2 and the (unversioned) CouchDBBundle supports the + compiler pass since the `CouchDB Mapping Compiler Pass pull request`_ + was merged. + + If you want your bundle to support older versions of Symfony and + Doctrine, you can provide a copy of the compiler pass in your bundle. + See for example the `FOSUserBundle mapping configuration`_ + ``addRegisterMappingsPass``. + 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; + use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass; + use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass; class FOSUserBundle extends Bundle { @@ -37,28 +49,89 @@ In your bundle class, write the following code to register the compiler pass:: $modelDir => 'FOS\UserBundle\Model', ); - $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection' - . '\Compiler\DoctrineOrmMappingsPass'; + $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass'; if (class_exists($ormCompilerClass)) { $container->addCompilerPass( DoctrineOrmMappingsPass::createXmlMappingDriver( - $mappings, 'fos_user.backend_type_orm' + $mappings, + 'fos_user.backend_type_orm' )); } - $mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection' - . '\Compiler\DoctrineMongoDBMappingsPass'; + $mongoCompilerClass = 'Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass'; if (class_exists($mongoCompilerClass)) { $container->addCompilerPass( DoctrineMongoDBMappingsPass::createXmlMappingDriver( - $mappings, 'fos_user.backend_type_mongodb' + $mappings, + 'fos_user.backend_type_mongodb' + )); + } + + $couchCompilerClass = 'Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass'; + if (class_exists($couchCompilerClass)) { + $container->addCompilerPass( + DoctrineCouchDBMappingsPass::createXmlMappingDriver( + $mappings, + 'fos_user.backend_type_couchdb' )); } - // TODO: couch + $phpcrCompilerClass = 'Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass'; + if (class_exists($phpcrCompilerClass)) { + $container->addCompilerPass( + DoctrinePhpcrMappingsPass::createXmlMappingDriver( + $mappings, + 'fos_user.backend_type_phpcr' + )); + } } } -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. +Note the :phpfunction:`class_exists` check. This is crucial, as you do not want your +bundle to have a hard dependency on all Doctrine bundles but let the user +decide which to use. + +The compiler pass provides factory methods for all drivers provided by Doctrine: +Annotations, XML, Yaml, PHP and StaticPHP. The arguments are: + +* a map of absolute directory path to namespace; +* an array of container parameters that your bundle uses to specify the name of + the Doctrine manager that it is using. The compiler pass will append the + parameter Doctrine is using to specify the name of the default manager. The + first parameter found is used and the mappings are registered with that + manager; +* an optional container parameter name that will be used by the compiler + pass to determine if this Doctrine type is used at all (this is relevant if + your user has more than one type of Doctrine bundle installed, but your + bundle is only used with one type of Doctrine. + +.. note:: + + The factory method is using the ``SymfonyFileLocator`` of Doctrine, meaning + it will only see XML and YML mapping files if they do not contain the + namespace. If you also need to map a base class, you can register a + compiler pass with the ``DefaultFileLocator`` like this:: + + private function buildMappingCompilerPass() + { + $arguments = array(array(realpath(__DIR__ . '/Resources/config/doctrine-base')), '.orm.xml'); + $locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator', $arguments); + $driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', array($locator)); + + return new DoctrineOrmMappingsPass( + $driver, + array('Full\Namespace'), + array('your_bundle.manager_name'), + 'your_bundle.orm_enabled' + ); + } + + And place your mapping file into ``/Resources/config/doctrine-base`` with the + fully qualified class name, separated by ``.`` instead of ``\``, for example + ``Other.Namespace.Model.Name.orm.xml``. You may not mix the two as otherwise + the SymfonyFileLocator will get confused. + + Adjust accordingly for the other Doctrine implementations. + +.. _`CouchDB Mapping Compiler Pass pull request`: https://github.com/doctrine/DoctrineCouchDBBundle/pull/27 +.. _`FOSUserBundle mapping configuration`: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserBundle.php pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy