在隔离捆绑包中注册 Symfony Hydrators



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

这个MyBundle包含几个定制的水合器。鉴于原则配置包含在特定应用程序中,并且我不希望必须在其 config.yml 文件中注册捆绑包水化器,我如何在捆绑包中注册这些水合器?

我尝试在我的捆绑包中创建编译器通行证,然后在原则或配置服务上调用addCustomHydrationMode,但这不起作用。

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

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

<?php
namespace VivaitUserBundleDependencyInjection;
use SymfonyComponentDependencyInjectionCompilerCompilerPassInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;
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', 'VivaitUserBundleAdapterHydratorCustomerHydrator']
        );
    }
}

最新更新