如何编写一个依赖于Doctrine并提供实体的symfony bundle?

uubf1zoe  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(89)

我正在尝试编写一个邮件队列捆绑包,用于将电子邮件事件存储到一个位置,并在稍后检索和处理它们,因为我们不使用mandrill等服务。
对于(这里对我的具体用例并不真实的感兴趣),我喜欢在我的bundle中提供额外的实体,因为我的bundle提供了一个BufferedDatabaseMailQueue。
由于一些研究,我在我的bundle的config.yml中包含了以下(尚未测试的)行:

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

字符串
最后,我得到了这个错误消息:
YamlFileLoader.php第404行中出现InvalidArgumentException:没有扩展可以加载“doctrine”的配置
研究表明,该PrependExtensionInterface可能会以某种方式帮助我.但我不知道如何正确使用和配置它.因此,我的捆绑包可以基于学说.
我该怎么做?

t3psigkw

t3psigkw1#

我使用以下代码管理它:

<?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
                )
            );
        }

    }
}

字符串

roejwanj

roejwanj2#

我想留下一个评论,但它太长了,所以我决定把它作为一个单独的答案。
首先,很明显,如果您使用属性而不是注解,请使用DoctrineOrmMappingsPass::createAttributeMappingDriver而不是DoctrineOrmMappingsPass::createAnnotationMappingDriver
其次,如果你使用的代码是,composer将失败与以下错误:

Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 1
!!  
!!  In NotSupported.php line 31:
!!                                                                                 
!!    Context: Using short namespace alias "MyMailQueueBundle" by calling Doctrine\O  
!!    RM\Configuration::addEntityNamespace                                         
!!    Problem: Feature was deprecated in doctrine/persistence 2.x and is not supp  
!!    orted by installed doctrine/persistence:3.x                                  
!!    Solution: See the doctrine/deprecations logs for new alternative approaches
!!

字符串
原因是OP在他的回答中设置了aliasMap
第三,我大量使用了领域驱动设计(DDD),我的实体被分隔在它们自己的文件夹中,在$directories中定义它们很好。
所以Symfony 6.3的改进解决方案是这样的:

<?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 = ['MyMailQueueBundle',];
            $directories = [
                realpath(__DIR__.'/User/Entity'),
                realpath(__DIR__.'/Common/Entity'),
                realpath(__DIR__.'/SomeOtherDir/Entity'),
            ];
            $managerParameters = array();
            $enabledParameter = false;
            $aliasMap = [];
            $container->addCompilerPass(
                DoctrineOrmMappingsPass::createAnnotationMappingDriver(
                        $namespaces,
                        $directories,
                        $managerParameters,
                        $enabledParameter,
                        $aliasMap,
                )
            );
        }

    }
}


我现在很满意

相关问题