【问题标题】:Custom doctrine hydrator as a symfony service自定义学说 hydrator 作为 symfony 服务
【发布时间】:2017-04-07 07:19:48
【问题描述】:

我按照以下教程进行了自定义补水。 https://techpunch.co.uk/development/create-custom-doctrine2-hydrator-symfony2

我对此不满意。我不希望我的捆绑包的用户需要在 config.yml 中配置水合器。

是否可以将其添加为服务并对其进行标记,例如

<service id="bundle.hydration.custom" class="Your\Bundle\Hydrators\ListHydrator">
    <argument type="service" id="helper_service" />

    <tag name="doctrine.hydrator" alias="ListHydrator" />
</service>

我还需要在我的 hydrator 中使用其他服务,所以我需要将其注册为服务。

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:

    如果自定义的hydrator除了实体管理器之外没有依赖,那么可以在容器编译时获取"doctrine.orm% name% _configuration"的定义并注入类名:

    $container
        ->getDefinition('doctrine.orm.some_configuration') // replace "some" by your entity manager name
        ->addMethodCall('addCustomHydrationMode', [
            'custom_custom', 'SomeCustomHydratorClass'
        ]);
    

    但是如果 hydrator 依赖于其他服务,那就是一个问题:Doctrine 实例化一个 hydrator 本身:

    // Doctrine\ORM\EntityManager::newHydrator($hydrationMode)
    
    if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) {
        return new $class($this);
    }
    

    所以,也许您需要 create a custom EntityManager 并覆盖 newHydrator() 方法。

    希望这会有所帮助。

    【讨论】:

    • 谢谢!但是想一想,我需要另辟蹊径,自定义实体管理器不是我想要依赖的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2018-08-15
    相关资源
    最近更新 更多