Search code examples
phpdoctrine-ormsymfony

How can I write a symfony bundle which relies on Doctrine and provides entities?


I am trying to write a mail-queue bundle for storing e-mail events into a location and retreive them later and process them, as we do not use a service like mandrill or likewise.

That for (and my exact usecase is not of real interest here), I like to provide additional entities in my bundle, as my bundle ships a BufferedDatabaseMailQueue.

Due to some research I included the following (yet untested) lines in the config.yml of my bundle:

doctrine:
orm:
    auto_mapping: false
    mappings:
       AcmeDemoBundle:
          type: annotation
          alias: MyMailQueueBundle
          prefix: MyMailQueueBundle\Entity
          dir: %kernel.root_dir%/../src/MyMailQueueBundle/Entity
          is_bundle: true

Anyways I end up with this error message:

InvalidArgumentException in YamlFileLoader.php line 404: There is no extension able to load the configuration for "doctrine"

Research indicated, that the PrependExtensionInterface might somehow help me. But I do not know how to correctly use and configure it. So that my Bundle can be based on doctrine.

How do I do that?


Solution

  • I managed it using this code:

    <?php
    
    namespace AltergearMailQueueBundle;
    
    use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    class MyMailQueueBundle extends Bundle
    {
        public function build(ContainerBuilder $container)
        {
            /*
             * To extend the bundle to work with mongoDB and couchDB you can follow this tutorial
             * http://symfony.com/doc/current/doctrine/mapping_model_classes.html
             * */
    
            parent::build($container);
            $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
    
            if (class_exists($ormCompilerClass))
            {
    
                $namespaces = array( 'MyMailQueueBundle\Entity' );
                $directories = array( realpath(__DIR__.'/Entity') );
                $managerParameters = array();
                $enabledParameter = false;
                $aliasMap = array('MyMailQueueBundle' => 'MyMailQueueBundle\Entity');
                $container->addCompilerPass(
                    DoctrineOrmMappingsPass::createAnnotationMappingDriver(
                            $namespaces,
                            $directories,
                            $managerParameters,
                            $enabledParameter,
                            $aliasMap
                    )
                );
            }
    
    
        }
    }