2016-11-23 67 views
1

我没有按照以下教程进行自定义水合作用。 https://techpunch.co.uk/development/create-custom-doctrine2-hydrator-symfony2作为symfony服务的自定义主义水合物

我不满意它。我不希望我的包的用户需要在config.yml中配置hydrator。

是否可以将其作为服务添加并标记为例如

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

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

另外我需要其他服务在我的hydrator所以我需要注册为服务。

回答

1

如果自定义的水化具有除了实体管理器没有依赖关系,那么你就可以容器编译过程中获得的"doctrine.orm% name% _configuration"的定义和注入类的名称:

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

但如果水化依赖于其他服务这是一个问题:学说实例化一个hydrators本身:

// Doctrine\ORM\EntityManager::newHydrator($hydrationMode) 

if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) { 
    return new $class($this); 
} 

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

希望这会有所帮助。

+0

谢谢!但想一想,我需要以另一种方式定制实体经理,而不是我想要的。 –