How to override bundled Doctrine repository in Symfony(如何在 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 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Symfony 中覆盖捆绑的 Doctrine 存储库
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01