• <tfoot id='XB1LT'></tfoot>
  • <small id='XB1LT'></small><noframes id='XB1LT'>

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

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

        如何在 Symfony 中覆盖捆绑的 Doctrine 存储库

        时间:2024-08-15

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

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

            1. <tfoot id='eCCNn'></tfoot><legend id='eCCNn'><style id='eCCNn'><dir id='eCCNn'><q id='eCCNn'></q></dir></style></legend>

                <tbody id='eCCNn'></tbody>

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

                • 本文介绍了如何在 Symfony 中覆盖捆绑的 Doctrine 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个独立的 Symfony 包(与 Composer 一起安装),其中包含实体和存储库,可在连接同一数据库的应用程序之间共享.

                  使用配置(显示 yml)将实体附加到每个应用程序:

                  原则:形式:映射:极致:类型:注解目录:%kernel.root_dir%/../vendor/acme/entities/src/Entities前缀:AcmeEntity别名:Acme

                  嗯,这是在应用程序中包含外部实体的最简单方法,但看起来有点难看.

                  每当我从实体管理器获得存储库时:

                  $entityManager->getRepository('Acme:User');

                  我得到预配置的存储库(在实体配置中)或默认的 DoctrineORMEntityRepository.

                  现在我想覆盖单个实体的捆绑(或默认)存储库类.有没有机会通过一些配置/扩展/等来做到这一点?

                  我认为,最好看的方式是:

                  原则:形式:.....:顶点实体用户:存储库类:我的超级存储库

                  或者带标签:

                  my.super.repository:类:我的超级存储库标签:- {名称:教义.custom.repository,实体:AcmeEntitiesUser }

                  解决方案

                  可以使用LoadClassMetadata事件:

                  类 LoadClassMetadataSubscriber 实现 EventSubscriber{/*** @inheritdoc*/公共函数 getSubscribedEvents(){返回 [事件::loadClassMetadata];}/*** @param LoadClassMetadataEventArgs $eventArgs*/公共函数 loadClassMetadata(LoadClassMetadataEventArgs $eventArgs){/*** @var DoctrineORMMappingClassMetadata $classMetadata*/$classMetadata = $eventArgs->getClassMetadata();if ($classMetadata->getName() !== 'AcmeEntitiesUser') {返回;}$classMetadata->customRepositoryClassName = 'MySuperRepository';}}

                  教义事件p><块引用>

                  使用配置(显示 yml)将实体附加到每个应用程序:嗯,这是在应用程序中包含外部实体的最简单方法,但看起来有点难看.

                  您可以启用 auto_mapping

                  I have an independent Symfony bundle (installed with Composer) with entities and repositories to share between my applications that connect same database.

                  Entities are attached to every applications using configuration (yml shown):

                  doctrine:
                      orm:
                          mappings:
                              acme:
                                  type: annotation
                                  dir: %kernel.root_dir%/../vendor/acme/entities/src/Entities
                                  prefix: AcmeEntities
                                  alias: Acme
                  

                  Well, it was the easiest way to include external entities in application, but looks a bit ugly.

                  Whenever I get repository from entity manager:

                  $entityManager->getRepository('Acme:User');
                  

                  I get either preconfigured repository (in entity configuration) or default DoctrineORMEntityRepository.

                  Now I want to override bundled (or default) repository class for a single entity. Is there any chance to do it with some configuration/extension/etc?

                  I think, the best looking way is something like:

                  doctrine:
                      orm:
                           ....:
                               AcmeEntitiesUser:
                                   repositoryClass: MySuperRepository
                  

                  Or with tags:

                  my.super.repository:
                      class: MySuperRepository
                      tags:
                          - { name: doctrine.custom.repository, entity: AcmeEntitiesUser }
                  

                  解决方案

                  You can use LoadClassMetadata event:

                  class LoadClassMetadataSubscriber implements EventSubscriber
                  {
                  
                      /**
                       * @inheritdoc
                       */
                      public function getSubscribedEvents()
                      {
                          return [
                              Events::loadClassMetadata
                          ];
                      }
                  
                      /**
                       * @param LoadClassMetadataEventArgs $eventArgs
                       */
                      public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
                      {
                          /**
                           * @var DoctrineORMMappingClassMetadata $classMetadata
                           */
                          $classMetadata = $eventArgs->getClassMetadata();
                  
                          if ($classMetadata->getName() !== 'AcmeEntitiesUser') {
                              return;
                          }
                  
                          $classMetadata->customRepositoryClassName = 'MySuperRepository';
                      }
                  
                  }
                  

                  Doctrine Events

                  Entities are attached to every applications using configuration (yml shown): Well, it was the easiest way to include external entities in application, but looks a bit ugly.

                  You can enable auto_mapping

                  这篇关于如何在 Symfony 中覆盖捆绑的 Doctrine 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Doctrine OneToOne 身份通过刷新时的外国实体异常 下一篇:与 Symfony 和 Doctrine 的 ON DELETE CASCADE 的多对多关系

                  相关文章

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

                    • <bdo id='3ADzH'></bdo><ul id='3ADzH'></ul>
                  1. <small id='3ADzH'></small><noframes id='3ADzH'>

                    <tfoot id='3ADzH'></tfoot>