api-skeletons/zf-doctrine-repository

Doctrine 仓库的插件架构

1.0.3 2018-04-05 04:06 UTC

This package is auto-updated.

Last update: 2024-09-11 14:41:04 UTC


README

Build Status Gitter Total Downloads

这是 Doctrine ORM 默认仓库结构的替代品。这个替代品实现了仓库的插件架构,以扩展仓库。

例如,如果您需要在仓库内部访问加密/解密资源,您可以将其作为插件实现,并可通过以下方式访问:

$this->plugin('encryption')->encrypt($value);

为什么使用这种仓库结构?

Doctrine ORM 的默认仓库没有提供访问 Doctrine 之外资源的权限。而且 Doctrine ORM 对象管理器也不提供访问依赖注入容器的权限。因此,当应用程序需要从其仓库中获得更多功能时,唯一的选项是编写自己的启用依赖注入的仓库工厂。为了创建组织依赖注入仓库工厂的标准方式,这是一个可接受的解决方案。

安装

此模块的安装使用 composer。有关 composer 文档,请参阅 getcomposer.org

$ composer require api-skeletons/zf-doctrine-repository

安装完成后,将 ZF\Doctrine\Repository 添加到 config/application.config.phpconfig/modules.config.php 中的模块列表中。

zf-component-installer

如果您使用 zf-component-installer,则该插件会为您安装 zf-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,仓库工厂可以使用它。

创建插件

仓库插件服务定位器的配置键为 zf-doctrine-repository-plugin。您的插件必须实现 ZF\Doctrine\Repository\Plugin\PluginInterface

您的插件的 __construct 方法将接受一个数组,包括仓库和其他参数。访问仓库会为您提供访问 ObjectManager 的权限。

使用 测试布尔插件测试布尔插件配置 作为模板。

可用插件

zf-doctrine-repository-query-provider - zfcampus/zf-apigility-doctrine 包括查询提供者,这些提供者可以接受当前认证用户并向 QueryBuilder 对象添加复杂过滤器,以确定用户是否可以访问给定实体。这种过滤机制可以在整个应用程序中跨实体需要授权访问时使用。

use Database\Entity\User;

// Return a single User entity fetched by applying the User Query Provider to a given `$id`
$objectManager->getRepository(User::class)->plugin('queryProvider')->find($id);

未来插件计划

此仓库是面向未来的,并设计来满足今天的需要以及未来的需求。以下是将要开发的仓库插件示例:

zf-doctrine-repository-audit - 对于喜欢触发器应用程序,将在所有可访问的 Doctrine 实体上创建触发器结构。数据可以直接访问,或者使用插件访问审计数据

use Database\Entity\User;

// Return the date an entity was created using the audit trail.
$objectManager->getRepository(User::class)->plugin('audit')->getCreatedAt(User $userEntity);

// Return the date an entity was last modified using the audit trail.
$objectManager->getRepository(User::class)->plugin('audit')->getUpdatedAt(User $userEntity);

// Return the complete audit trail for an entity
$objectManager->getRepository(User::class)->plugin('audit')->getAuditTrail(User $userEntity);