在 Isolated Bundle 中注册 Symfony Hydrators

Register Symfony Hydrators in Isolated Bundle

我创建了几个模块化应用程序,它们共享 Bundle。这些包之一负责处理所有 entity/repository 相关逻辑(我们称之为 MyBundle)。

这个 MyBundle 包含几个自定义保湿器。鉴于特定应用程序中包含学说配置,并且我不希望必须在其 config.yml 文件中注册捆绑式水化器,我如何才能在捆绑中注册这些水化器?

我试过在我的包中创建一个 CompilerPass,然后在 doctrine orm 配置服务上调用 addCustomHydrationMode,但这不起作用。

我还尝试创建一个 'HydrationManager' class,注入实体管理器,然后在编译器通道中调用管理器上的 addHydrator 方法,后者又调用 getConfiguration()->addCustomHydrationMode(。 ..) 但这也没有用

我在寻找类似的解决方案,偶然发现了这个例子: 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']
        );
    }
}