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

2022-01-16 00:00:00 php symfony doctrine doctrine-orm

我有一个独立的 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

相关文章