zfcampus/zf-apigility-doctrine
最新稳定版本:2.3.0
Composer 安装命令:
composer require zfcampus/zf-apigility-doctrine
包简介
Apigility Doctrine module
README 文档
README
Repository abandoned 2019-12-31
This repository has moved to laminas-api-tools/api-tools-doctrine.
This module provides the classes for integrating Doctrine with Apigility.
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
$ composer require zfcampus/zf-apigility-doctrine This library provides two modules. The first, ZF\Apigility\Doctrine\Server provides the classes to serve data created by the second, ZF\Apigility\Doctrine\Admin. The Admin module is used to create apigility resources and the Server serves those created resources. Generally you would include Admin in your config/development.config.php and Server in your config/application.config.php.
ZF\Apigility\Doctrine\Server has a dependency with Phpro\DoctrineHydrationModule to handle entity hydration. See documentation and instructions on how to set up this module.
zend-component-installer
If you use zend-component-installer, that plugin will install zf-apigility-doctrine, and all modules it depends on, as a module in your application configuration for you.
For Apache installations it is recommended the AllowEncodedSlashes-directive is set to On so the configuration can be read.
API Resources
NOTE! This section was/is intended for the authors of zf-apigility-admin-ui. While it is possible to use these instructions to manually create Apigility Doctrine resources it is strongly recommended to use the UI.
/apigility/api/doctrine[/:object_manager_alias]/metadata[/:name]
This will return metadata for the named entity which is a member of the named object manager. Querying without a name will return all metadata for the object manager.
/apigility/api/module[/:name]/doctrine[/:controller_service_name]
This is a Doctrine resource route like Apigility Rest /apigility/api/module[/:name]/rest[/:controller_service_name] To create a resource do not include [/:controller_service_name]
POST Parameters
{ "objectManager": "doctrine.entitymanager.orm_default", "serviceName": "Artist", "entityClass": "Db\\Entity\\Artist", "routeIdentifierName": "artist_id", "entityIdentifierName": "id", "routeMatch": "/api/artist", "pageSizeParam": "limit", // optional, default null "hydratorName": "DbApi\\V1\\Rest\\Artist\\ArtistHydrator", // optional, default generated "hydrateByValue": true // optional, default true } Hydrating Entities by Value or Reference
By default the admin tool hydrates entities by reference by setting $config['doctrine-hydrator']['hydrator_class']['by_value'] to false.
Custom Events
It is possible to hook in on specific doctrine events of the type DoctrineResourceEvent. This way, it is possible to alter the doctrine entities or collections before or after a specific action is performed.
Supported events:
EVENT_FETCH_PRE = 'fetch.pre'; EVENT_FETCH_POST = 'fetch.post'; EVENT_FETCH_ALL_PRE = 'fetch-all.pre'; EVENT_FETCH_ALL_POST = 'fetch-all.post'; EVENT_CREATE_PRE = 'create.pre'; EVENT_CREATE_POST = 'create.post'; EVENT_UPDATE_PRE = 'update.pre'; EVENT_UPDATE_POST = 'update.post'; EVENT_PATCH_PRE = 'patch.pre'; EVENT_PATCH_POST = 'patch.post'; EVENT_PATCH_LIST_PRE = 'patch-all.pre'; EVENT_PATCH_LIST_POST = 'patch-all.post'; EVENT_DELETE_PRE = 'delete.pre'; EVENT_DELETE_POST = 'delete.post'; EVENT_DELETE_LIST_PRE = 'delete-list.pre'; EVENT_DELETE_LIST_POST = 'delete-list.post'; Attach to events through the Shared Event Manager:
use ZF\Apigility\Doctrine\Server\Event\DoctrineResourceEvent; $sharedEvents = $this->getApplication()->getEventManager()->getSharedManager(); $sharedEvents->attach( 'ZF\Apigility\Doctrine\DoctrineResource', DoctrineResourceEvent::EVENT_CREATE_PRE, function(DoctrineResourceEvent $e) { $e->stopPropagation(); return new ApiProblem(400, 'Stop API Creation'); } );
It is also possible to add custom event listeners to the configuration of a single doctrine-connected resource:
'zf-apigility' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\User\\UserResource' => [ // ... 'listeners' => [ 'key.of.aggregate.listener.in.service_manager', ], ], ], ],
Querying Single Entities
Multi-keyed entities
You may delimit multi keys through the route parameter. The default delimiter is a period . (e.g. 1.2.3). You may change the delimiter by setting the DoctrineResource::setMultiKeyDelimiter($value).
Complex queries through route parameters
NO LONGER SUPPORTED. As of version 2.0.4 this functionality has been removed from this module. The intended use of this module is a 1:1 mapping of entities to resources and using subroutes is not in the spirit of this intention. It is STRONGLY recommended you use zfcampus/zf-doctrine-querybuilder for complex query-ability.
Query Providers
Query Providers are available for all find operations. The find query provider is used to fetch an entity before it is acted upon for all DoctrineResource methods except create.
A query provider returns a QueryBuilder object. By using a custom query provider you may inject conditions specific to the resource or user without modifying the resource. For instance, you may add a $queryBuilder->andWhere('user = ' . $event->getIdentity()); in your query provider before returning the QueryBuilder created therein. Other uses include soft deletes so the end user can only see the active records.
A custom plugin manager is available to register your own query providers. This can be done through this configuration:
'zf-apigility-doctrine-query-provider' => [ 'aliases' => [ 'entity_name_fetch_all' => \Application\Query\Provider\EntityName\FetchAll::class, ], 'factories' => [ \Application\Query\Provider\EntityName\FetchAll::class => \Zend\ServiceManager\Factory\InvokableFactory::class, ], ],
When the query provider is registered attach it to the doctrine-connected resource configuration. The default query provider is used if no specific query provider is set. You may set query providers for these keys:
- default
- fetch
- fetch_all
- update
- patch
- delete
'zf-apigility' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\....' => [ 'query_providers' => [ 'default' => 'default_orm', 'fetch_all' => 'entity_name_fetch_all', // or fetch, update, patch, delete ], ], ], ],
Query Create Filters
In order to filter or change data sent to a create statement before it is used to hydrate the entity you may use a query create filter. Create filters are very similar to Query Providers in their implementation.
Create filters take the data as a parameter and return the data, modified or filtered.
A custom plugin manager is available to register your own create filters. This can be done through following configuration:
'zf-apigility-doctrine-query-create-filter' => [ 'aliases' => [ 'entity_name' => \Application\Query\CreateFilter\EntityName::class, ], 'factories' => [ \Application\Query\CreateFilter\EntityName::class => \Zend\ServiceManager\Factory\InvokableFactory::class, ], ],
Register your Query Create Filter as:
'zf-apigility' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\....' => [ 'query_create_filter' => 'entity_name', ... ], ], ],
Using Entity Factories
By default, Doctrine entities are instantiated by FQCN without arguments. If you need anything different than that, for example if your entities require arguments in their constructors, you may specify the name of a Doctrine\Instantiator\InstantiatorInterface factory registered in the Service Manager in order to delegate instantiation to that service. Currently this can only be done by directly editing the config for your resources as follows:
'zf-apigility' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\...Resource' => [ 'entity_factory' => 'key_in_service_manager', ... ], ], ],
zfcampus/zf-apigility-doctrine 适用场景与选型建议
zfcampus/zf-apigility-doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 450.47k 次下载、GitHub Stars 达 111, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「doctrine」 「ZendFramework」 「zf」 「apigility」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zfcampus/zf-apigility-doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zfcampus/zf-apigility-doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zfcampus/zf-apigility-doctrine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Make pimcore migration simple
A module manager for Zend Framework which can be used to create configs per domain.
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
ZF2 module for Doctrine 1.x integration
Use FileMaker through CWP as your backend database
统计信息
- 总下载量: 450.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 111
- 点击次数: 19
- 依赖项目数: 12
- 推荐数: 3
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04