Skip to content

update cookbook for couch and phpcr and give some more hints and background #2669

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 1 commit into from
Jun 30, 2013
Merged
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
97 changes: 85 additions & 12 deletions cookbook/doctrine/mapping_model_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@beberlei do you have any input on this, does it make sense to recommend this solution?


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