<small id='0qqmv'></small><noframes id='0qqmv'>

  • <i id='0qqmv'><tr id='0qqmv'><dt id='0qqmv'><q id='0qqmv'><span id='0qqmv'><b id='0qqmv'><form id='0qqmv'><ins id='0qqmv'></ins><ul id='0qqmv'></ul><sub id='0qqmv'></sub></form><legend id='0qqmv'></legend><bdo id='0qqmv'><pre id='0qqmv'><center id='0qqmv'></center></pre></bdo></b><th id='0qqmv'></th></span></q></dt></tr></i><div id='0qqmv'><tfoot id='0qqmv'></tfoot><dl id='0qqmv'><fieldset id='0qqmv'></fieldset></dl></div>
    • <bdo id='0qqmv'></bdo><ul id='0qqmv'></ul>

    <legend id='0qqmv'><style id='0qqmv'><dir id='0qqmv'><q id='0qqmv'></q></dir></style></legend>

      <tfoot id='0qqmv'></tfoot>

        使用自定义 Doctrine 2 hydrator 进行依赖注入

        Dependency injection with custom Doctrine 2 hydrator(使用自定义 Doctrine 2 hydrator 进行依赖注入)

            <bdo id='6FLIq'></bdo><ul id='6FLIq'></ul>

                  <tbody id='6FLIq'></tbody>
                • <legend id='6FLIq'><style id='6FLIq'><dir id='6FLIq'><q id='6FLIq'></q></dir></style></legend>

                • <tfoot id='6FLIq'></tfoot>
                • <small id='6FLIq'></small><noframes id='6FLIq'>

                  <i id='6FLIq'><tr id='6FLIq'><dt id='6FLIq'><q id='6FLIq'><span id='6FLIq'><b id='6FLIq'><form id='6FLIq'><ins id='6FLIq'></ins><ul id='6FLIq'></ul><sub id='6FLIq'></sub></form><legend id='6FLIq'></legend><bdo id='6FLIq'><pre id='6FLIq'><center id='6FLIq'></center></pre></bdo></b><th id='6FLIq'></th></span></q></dt></tr></i><div id='6FLIq'><tfoot id='6FLIq'></tfoot><dl id='6FLIq'><fieldset id='6FLIq'></fieldset></dl></div>
                  本文介绍了使用自定义 Doctrine 2 hydrator 进行依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在 Symfony 2 项目中的 Doctrine 2 中设置自定义水合器,但要让它执行所需的操作,需要其他服务.自定义水化器的文档 只展示了如何提供 hydrator 类,因此无法注入依赖项.

                  I'm setting up a custom hydrator in Doctrine 2 in a Symfony 2 project, but for it to do what it needs it requires another service. The documentation for custom hydrators only shows how to provide a hydrator class, so there's no way to inject dependencies.

                  例如:

                  $em->getConfiguration()->addCustomHydrationMode('CustomHydrator', 'MyProjectHydratorsCustomHydrator');
                  

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

                  I suspect Doctrine is initialising the hydrators itself and as such any dependencies would need to be passed through some other Doctrine classes first.

                  有没有办法提供一个自定义的水化工厂"或类似于 Doctrine 的东西来允许注入额外的依赖项?如果没有此功能,定制水化器似乎相当有限.

                  Is there a way to provide a custom "hydration factory" or similar to Doctrine that would allow injection of additional dependencies? Custom hydrators seem fairly limited without this capability.

                  答案:感谢 Denis V

                  我按如下方式工作.我无法发布实际代码,因此我将一些虚拟占位符放在一起,以便您了解它们是如何组合在一起的.

                  I got this working as follows. I can't post the actual code so I've put together some dummy placeholders so you can see how it fits together.

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

                  services:
                      doctrine.orm.entity_manager.abstract:
                          class:          AcmeExampleBundleEntityDoctrineEntityManager
                          factory_class:  AcmeExampleBundleEntityDoctrineEntityManager
                          factory_method: create
                          abstract:       true
                          calls:
                              - [ setMyDependency, [@acme.my_custom_service]]
                  

                  src/Acme/ExampleBundle/Entity/DoctrineEntityManager.php

                  namespace AcmeExampleBundleEntity;
                  
                  use AcmeExampleBundleHydratorMyHydrator;
                  use DoctrineCommonEventManager;
                  use DoctrineDBALConnection;
                  use DoctrineORMConfiguration;
                  use DoctrineORMEntityManager as BaseEntityManager;
                  use DoctrineORMORMException;
                  use DoctrineORMQuery;
                  
                  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 = DoctrineDBALDriverManager::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 AcmeExampleBundleHydrator;
                  
                  use DoctrineORMEntityManager;
                  use DoctrineORMInternalHydrationObjectHydrator;
                  
                  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 中添加这个

                  Try adding this in your config.yml

                  doctrine:
                      orm:
                          hydrators:
                              CustomHydrator: MyProjectHydratorsCustomHydrator
                  

                  更新

                  由于您无法向 Hydrator 本身注入任何内容,您可以改为创建自定义 EntityManager(您自己建议).

                  As you cannot inject anything to the Hydrator itself, you can instead create a custom EntityManager (that you suggested yourself).

                  可以这样做:

                  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"] ]
                  

                  这篇关于使用自定义 Doctrine 2 hydrator 进行依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <tfoot id='QbPfD'></tfoot>
                    <tbody id='QbPfD'></tbody>
                      <legend id='QbPfD'><style id='QbPfD'><dir id='QbPfD'><q id='QbPfD'></q></dir></style></legend>

                      <i id='QbPfD'><tr id='QbPfD'><dt id='QbPfD'><q id='QbPfD'><span id='QbPfD'><b id='QbPfD'><form id='QbPfD'><ins id='QbPfD'></ins><ul id='QbPfD'></ul><sub id='QbPfD'></sub></form><legend id='QbPfD'></legend><bdo id='QbPfD'><pre id='QbPfD'><center id='QbPfD'></center></pre></bdo></b><th id='QbPfD'></th></span></q></dt></tr></i><div id='QbPfD'><tfoot id='QbPfD'></tfoot><dl id='QbPfD'><fieldset id='QbPfD'></fieldset></dl></div>

                          <small id='QbPfD'></small><noframes id='QbPfD'>

                            <bdo id='QbPfD'></bdo><ul id='QbPfD'></ul>