api-skeletons / doctrine-repository
Doctrine Repositories 的插件架构
Requires
- php: ^7.3
- doctrine/doctrine-orm-module: ^3.0 || ^4.0
- gianarb/angry: ^1.0
- laminas/laminas-servicemanager: ^3.6
- phpunit/phpunit: ^9.5
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-29 06:01:40 UTC
README
这是 Doctrine ORM 默认仓库结构的替代品。此替代品实现了对仓库扩展的插件架构。
例如,如果您需要在仓库中访问加密/解密资源,您可以将它作为插件实现,以便通过以下方式访问:
$this->plugin('encryption')->encrypt($value);
为什么使用这种仓库结构?
Doctrine ORM 的默认仓库没有提供访问 Doctrine 外部资源的方法。同时,Doctrine ORM 对象管理器也不提供访问依赖注入容器的权限。因此,当您的应用程序需要从其仓库中获取更多信息时,唯一的选择是编写自己的依赖注入仓库工厂。为了创建一个组织依赖注入仓库工厂的标准方法:这是一个可接受的解决方案。
安装
此模块的安装使用 composer。有关 composer 文档,请参阅 getcomposer.org。
$ composer require api-skeletons/doctrine-repository
安装后,将 ZF\Doctrine\Repository
添加到 config/application.config.php
或 config/modules.config.php
中的模块列表中。
laminas-component-installer
如果您使用 laminas-component-installer,则该插件将帮助您将 doctrine-repository 作为模块进行安装。
配置
使用此模块不需要手动配置。
此模块会对您的 doctrine.entitymanager.orm_default
配置进行以下更改
namespace ZF\Doctrine\Repository;
...
'doctrine' => [
'configuration' => [
'orm_default' => [
'repository_factory' => RepositoryFactory::class,
'default_repository_class_name' => ObjectRepository::class,
],
],
],
如果您的应用程序已经有一个默认的仓库类,您可以编辑它以实现 ZF\Doctrine\Repository\ObjectRepositoryInterface
,然后仓库工厂可以使用它。
创建插件
仓库插件服务定位器的配置键为 api-skeletons-doctrine-repository-plugin
。您的插件必须实现 ZF\Doctrine\Repository\Plugin\PluginInterface
您的插件的 __construct
方法将接收一个数组,包括仓库和其他任何参数。对仓库的访问将为您提供对 ObjectManager 的访问权限。