gpt4 book ai didi

php - 使用自定义 Doctrine 2 hydrator 进行依赖注入(inject)

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:38:32 24 4
gpt4 key购买 nike

我正在 Symfony 2 项目中的 Doctrine 2 中设置自定义水化器,但要使其执行所需操作,它需要另一项服务。 documentation for custom hydrators只展示了如何提供一个 hydrator 类,所以没有办法注入(inject)依赖。

例如:

$em->getConfiguration()->addCustomHydrationMode('CustomHydrator', 'MyProject\Hydrators\CustomHydrator');

我怀疑 Doctrine 正在初始化水化器本身,因此任何依赖项都需要首先通过其他一些 Doctrine 类传递。

有没有办法提供自定义的“水化工厂”或类似于允许注入(inject)额外依赖项的 Doctrine?如果没有此功能,定制保湿器似乎相当有限。


答案:感谢 Denis V

我的工作如下。我无法发布实际代码,所以我将一些虚拟占位符放在一起,以便您可以看到它们是如何组合在一起的。

src/Acme/ExampleBundle/resources/config/services.yml

services:
doctrine.orm.entity_manager.abstract:
class: Acme\ExampleBundle\Entity\DoctrineEntityManager
factory_class: Acme\ExampleBundle\Entity\DoctrineEntityManager
factory_method: create
abstract: true
calls:
- [ setMyDependency, [@acme.my_custom_service]]

src/Acme/ExampleBundle/Entity/DoctrineEntityManager.php

namespace Acme\ExampleBundle\Entity;

use Acme\ExampleBundle\Hydrator\MyHydrator;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager as BaseEntityManager;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query;

class DoctrineEntityManager extends BaseEntityManager
{
protected $myDependency;

/**
* Note: This must be redefined as Doctrine's own entity manager has its own class name hardcoded in.
*/
public static function create($conn, Configuration $config, EventManager $eventManager = null)
{
if (!$config->getMetadataDriverImpl()) {
throw ORMException::missingMappingDriverImpl();
}

switch (true) {
case (is_array($conn)):
$conn = \Doctrine\DBAL\DriverManager::getConnection(
$conn, $config, ($eventManager ?: new EventManager())
);
break;

case ($conn instanceof Connection):
if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
throw ORMException::mismatchedEventManager();
}
break;

default:
throw new \InvalidArgumentException("Invalid argument: " . $conn);
}

return new self($conn, $config, $conn->getEventManager());
}

public function setMyDependency($myCustomService)
{
$this->myDependency = $myCustomService;
}

public function newHydrator($hydrationMode)
{
if ($hydrationMode == 'MyHydrationMode') {
return new MyHydrator($this, $this->myDependency);
}

return parent::newHydrator($hydrationMode);
}
}

src/Acme/ExampleBundle/Hydrator/MyHydrator.php

namespace Acme\ExampleBundle\Hydrator;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;

class MyHydrator extends ObjectHydrator
{
protected $myDependency;

public __construct(EntityManager $em, $myDependency)
{
parent::__construct($em);

$this->myDependency = $myDependency;
}

protected function hydrateAllData()
{
/* hydration stuff with my dependency here */
}
}

最佳答案

尝试在你的 config.yml 中添加这个

doctrine:
orm:
hydrators:
CustomHydrator: MyProject\Hydrators\CustomHydrator

更新

由于您不能向 Hydrator 本身注入(inject)任何东西,因此您可以创建一个自定义的 EntityManager(您自己建议的)。

可以这样做:

services:  
name_of_your_custom_manager:
class: %doctrine.orm.entity_manager.class%
factory_service: doctrine
factory_method: getManager
arguments: ["name_of_your_custom_manager"]
calls:
- [ setCustomDependency, ["@acme_bundle.custom_dependency"] ]

关于php - 使用自定义 Doctrine 2 hydrator 进行依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24330930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com