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

问题描述 投票:0回答:2

我正在尝试编写一个邮件队列包,用于将电子邮件事件存储到某个位置并稍后检索并处理它们,因为我们不使用像 mandrill 或类似的服务。

为此(我的确切用例在这里并不真正感兴趣),我喜欢在我的包中提供额外的实体,因为我的包提供了 BufferedDatabaseMailQueue。

由于一些研究,我在我的包的 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可能会以某种方式帮助我。但我不知道如何正确使用和配置它。这样我的 Bundle 就能以教义为基础。

我该怎么做?

php doctrine-orm symfony
2个回答
4
投票

我使用此代码管理它:

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


    }
}

0
投票

我想过发表评论,但太长了,所以我决定将其作为单独的答案发布。

首先,显然,如果您使用属性而不是注释,请使用

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


    }
}

现在对我来说效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.