0

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

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

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

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

4

1 回答 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']
        );
    }
}
于 2016-09-14T15:18:03.457 回答