首页 文章

在隔离捆绑中注册Symfony Hydrators

提问于
浏览
0

我创建了几个模块化应用程序,它们共享Bundles . 其中一个bundle负责处理所有与实体/存储库相关的逻辑(我们称之为MyBundle) .

这款MyBundle包含多种定制保湿剂 . 如果学说配置包含在特定的应用程序中,我怎么能在捆绑中注册这些水合器,我不希望在config.yml文件中注册捆绑水合器?

我已经尝试在我的bundle中创建CompilerPass,然后在doctrine orm配置服务上调用addCustomHydrationMode,但这不起作用 .

我还尝试创建一个'HydrationManager'类,注入实体管理器,然后在编译器传递中调用管理器上的addHydrator方法,然后调用getConfiguration() - > addCustomHydrationMode(...)但这也行不通

1 回答

  • 2

    我一直在寻找类似的解决方案,偶然发现了这个例子:https://github.com/vivait/user-bundle/blob/master/src/Vivait/UserBundle/DependencyInjection/DoctrineCompilerPass.php

    <?php
    
    namespace Vivait\UserBundle\DependencyInjection;
    
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    
    class DoctrineCompilerPass implements CompilerPassInterface
    {
        /**
         * You can modify the container here before it is dumped to PHP code.
         *
         * @param ContainerBuilder $container
         *
         * @api
         */
        public function process(ContainerBuilder $container)
        {
            $ormConfigDef = $container->getDefinition('doctrine.orm.configuration');
            $ormConfigDef->addMethodCall(
                'addCustomHydrationMode',
                ['UserCustomerHydrator', 'Vivait\UserBundle\Adapter\Hydrator\CustomerHydrator']
            );
        }
    }
    

相关问题