foleon / api-tools-doctrine
Laminas API Tools Doctrine module
Requires
- php: ^7.4 || ~8.0.0 || ~8.1.0 || ~8.2.0
- ext-json: *
- foleon/doctrine-orm-hydration-module: ^1.0
- laminas-api-tools/api-tools: ^1.3
- laminas-api-tools/api-tools-rest: ^1.3.2
- laminas/laminas-hydrator: ^4.0
- laminas/laminas-servicemanager: ^3.11.2
- laminas/laminas-stdlib: ^3.3
- laminas/laminas-view: ^2.11.3
- laminas/laminas-zendframework-bridge: ^1.0
Requires (Dev)
- doctrine/dbal: ^2.10
- doctrine/doctrine-module: ^5.1
- doctrine/doctrine-orm-module: ^5.1
- laminas-api-tools/api-tools-admin: ^2.1
- laminas-api-tools/api-tools-hal: ^1.6.1
- laminas/laminas-coding-standard: ~2.3.0
- laminas/laminas-form: ^3.1.1
- laminas/laminas-i18n: ^2.7.3
- laminas/laminas-log: ^2.9.1
- laminas/laminas-mvc: ^2.7.13 || ^3.0.2
- laminas/laminas-serializer: ^2.8.0
- laminas/laminas-test: ^2.6.1 || ^3.0.1 || ^4.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.3
- psalm/plugin-phpunit: ^0.16.1
- symfony/yaml: ^2.3 || ^3.0 || ^4.0 || ^5.0
- vimeo/psalm: ^4.8
Suggests
- api-skeletons/zf-doctrine-hydrator: Hydrator strategies for Doctrine in Laminas API Tools
- api-skeletons/zf-oauth2-doctrine: OAuth2 Doctrine Adapter for Laminas API Tools
- doctrine/doctrine-mongo-odm-module: For Mongo ODM mapping
- doctrine/doctrine-orm-module: For ORM mapping
Conflicts
- doctrine/dbal: <2.12.0
- doctrine/orm: <2.6.3
Replaces
- zfcampus/zf-apigility-doctrine: ^2.3.0
This package is not auto-updated.
Last update: 2024-11-12 22:34:43 UTC
README
π·πΊ Π ΡΡΡΠΊΠΈΠΌ Π³ΡΠ°ΠΆΠ΄Π°Π½Π°ΠΌ
ΠΡ, ΡΡΠ°ΡΡΠ½ΠΈΠΊΠΈ Laminas, ΡΠΎΠ΄ΠΈΠ»ΠΈΡΡ ΠΈ ΠΆΠΈΠ²Π΅ΠΌ Π² ΡΠ°Π·Π½ΡΡ ΡΡΡΠ°Π½Π°Ρ . Π£ ΠΌΠ½ΠΎΠ³ΠΈΡ ΠΈΠ· Π½Π°Ρ Π΅ΡΡΡ Π΄ΡΡΠ·ΡΡ, ΡΠΎΠ΄ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΈ ΠΈ ΠΊΠΎΠ»Π»Π΅Π³ΠΈ ΠΊΠ°ΠΊ Π² Π ΠΎΡΡΠΈΠΈ, ΡΠ°ΠΊ ΠΈ Π² Π£ΠΊΡΠ°ΠΈΠ½Π΅. ΠΠ΅ΠΊΠΎΡΠΎΡΡΠ΅ ΠΈΠ· Π½Π°Ρ ΡΠΎΠ΄ΠΈΠ»ΠΈΡΡ Π² Π ΠΎΡΡΠΈΠΈ. ΠΠ΅ΠΊΠΎΡΠΎΡΡΠ΅ ΠΈΠ· Π½Π°Ρ ΠΆΠΈΠ²ΡΡ Π² Π ΠΎΡΡΠΈΠΈ. Π£ Π½Π΅ΠΊΠΎΡΠΎΡΡΡ Π±Π°Π±ΡΡΠΊΠΈ ΠΈ Π΄Π΅Π΄ΡΡΠΊΠΈ ΡΡΠ°ΠΆΠ°Π»ΠΈΡΡ Ρ ΡΠ°ΡΠΈΡΡΠ°ΠΌΠΈ Π²ΠΎ ΠΡΠΎΡΠΎΠΉ ΠΌΠΈΡΠΎΠ²ΠΎΠΉ Π²ΠΎΠΉΠ½Π΅. ΠΠ΄Π΅ΡΡ Π½ΠΈΠΊΡΠΎ Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΡΠ°ΡΠΈΠ·ΠΌ.
Π£ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· Π½Π°Ρ Π΅ΡΡΡ ΡΠΊΡΠ°ΠΈΠ½ΡΠΊΠ°Ρ ΡΠΎΠ΄ΡΡΠ²Π΅Π½Π½ΠΈΡΠ°, ΠΊΠΎΡΠΎΡΠ°Ρ ΡΠΏΠ°ΡΠ»Π°ΡΡ ΠΈΠ· Π΄ΠΎΠΌΠ° Π²ΠΌΠ΅ΡΡΠ΅ Ρ ΡΡΠ½ΠΎΠΌ. ΠΠΎΠ΅Π·Π΄ Π·Π°Π΄Π΅ΡΠΆΠ°Π»ΡΡ ΠΈΠ·-Π·Π° Π±ΠΎΠΌΠ±Π΅ΠΆΠΊΠΈ Π½Π° Π΄ΠΎΡΠΎΠ³Π΅ Π²ΠΏΠ΅ΡΠ΅Π΄ΠΈ. Π£ Π½Π°Ρ Π΅ΡΡΡ Π΄ΡΡΠ·ΡΡ, ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΡΡΡΡΡΡ Π² Π±ΠΎΠΌΠ±ΠΎΡΠ±Π΅ΠΆΠΈΡΠ°Ρ . ΠΡ Ρ ΡΡΠ΅Π²ΠΎΠ³ΠΎΠΉ ΠΆΠ΄Π΅ΠΌ Π²Π΅ΡΡΠΎΡΠΊΠΈ ΠΎΡ Π½ΠΈΡ ΠΏΠΎΡΠ»Π΅ Π²ΠΎΠ·Π΄ΡΡΠ½ΡΡ Π½Π°Π»Π΅ΡΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ Π±Π΅ΡΠΏΠΎΡΡΠ΄ΠΎΡΠ½ΠΎ Π½Π°Π½ΠΎΡΡΡ ΡΠ΄Π°ΡΡ ΠΈ ΠΏΠΎΠΏΠ°Π΄Π°ΡΡ ΠΏΠΎ Π±ΠΎΠ»ΡΠ½ΠΈΡΠ°ΠΌ, ΡΠΊΠΎΠ»Π°ΠΌ, Π΄Π΅ΡΡΠΊΠΈΠΌ ΡΠ°Π΄Π°ΠΌ ΠΈ Π΄ΠΎΠΌΠ°ΠΌ. ΠΡ Π½Π΅ Π±Π΅ΡΠ΅ΠΌ ΡΡΠΎ ΠΈΠ· ΠΊΠ°ΠΊΠΈΡ -Π»ΠΈΠ±ΠΎ Π‘ΠΠ. ΠΡ Π½Π°Π±Π»ΡΠ΄Π°Π΅ΠΌ ΡΡΠΎ Π½Π°ΠΏΡΡΠΌΡΡ.
ΠΡ Π΄ΠΎΠ²Π΅ΡΡΠ΅ΡΠ΅ Π½Π°ΠΌ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ, ΡΡΠΎΠ± ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π½Π°ΡΠΈ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, ΠΈ ΠΌΡ ΠΏΡΠΎΡΠΈΠΌ Π²Π°Ρ Π΄ΠΎΠ²Π΅ΡΠΈΡΡΡΡ Π½Π°ΠΌ Π²Π½ΠΎΠ²Ρ. ΠΡ Π½ΡΠΆΠ΄Π°Π΅ΠΌΡΡ Π² ΠΏΠΎΠΌΠΎΡΠΈ. ΠΡΡ ΠΎΠ΄ΠΈΡΠ΅ ΠΈ ΠΏΡΠΎΡΠ΅ΡΡΡΠΉΡΠ΅ ΠΏΡΠΎΡΠΈΠ² ΡΡΠΎΠΉ Π±Π΅ΡΠΏΠΎΠ»Π΅Π·Π½ΠΎΠΉ Π²ΠΎΠΉΠ½Ρ. ΠΡΡΠ°Π½ΠΎΠ²ΠΈΡΠ΅ ΠΊΡΠΎΠ²ΠΎΠΏΡΠΎΠ»ΠΈΡΠΈΠ΅. Π‘ΠΊΠ°ΠΆΠΈΡΠ΅ "ΠΠ΅Ρ Π²ΠΎΠΉΠ½Π΅!"
πΊπΈ To Citizens of Russia
We at Laminas come from all over the world. Many of us have friends, family and colleagues in both Russia and Ukraine. Some of us were born in Russia. Some of us currently live in Russia. Some have grandparents who fought Nazis in World War II. Nobody here supports fascism.
One team member has a Ukrainian relative who fled her home with her son. The train was delayed due to bombing on the road ahead. We have friends who are hiding in bomb shelters. We anxiously follow up on them after the air raids, which indiscriminately fire at hospitals, schools, kindergartens and houses. We're not taking this from any media. These are our actual experiences.
You trust us enough to use our software. We ask that you trust us to say the truth on this. We need your help. Go out and protest this unnecessary war. Stop the bloodshed. Say "stop the war!"
This module provides the classes for integrating Doctrine with Laminas API Tools.
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
$ composer require laminas-api-tools/api-tools-doctrine
This library provides two modules. The first, Laminas\ApiTools\Doctrine\Server
provides
the classes to serve data created by the second, Laminas\ApiTools\Doctrine\Admin
.
The Admin module is used to create api-tools 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
.
Laminas\ApiTools\Doctrine\Server
has a dependency with Phpro\DoctrineHydrationModule
to handle
entity hydration. See documentation and instructions
on how to set up this module.
laminas-component-installer
If you use laminas-component-installer, that plugin will install api-tools-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 api-tools-admin-ui. While it is possible to use these instructions to manually create Laminas API Tools Doctrine resources it is strongly recommended to use the UI.
/api-tools/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.
/api-tools/api/module[/:name]/doctrine[/:controller_service_name]
This is a Doctrine resource route like Laminas API Tools Rest /api-tools/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 Laminas\ApiTools\Doctrine\Server\Event\DoctrineResourceEvent; $sharedEvents = $this->getApplication()->getEventManager()->getSharedManager(); $sharedEvents->attach( 'Laminas\ApiTools\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:
'api-tools' => [ '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 laminas-api-tools/api-tools-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:
'api-tools-doctrine-query-provider' => [ 'aliases' => [ 'entity_name_fetch_all' => \Application\Query\Provider\EntityName\FetchAll::class, ], 'factories' => [ \Application\Query\Provider\EntityName\FetchAll::class => \Laminas\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
'api-tools' => [ '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:
'api-tools-doctrine-query-create-filter' => [ 'aliases' => [ 'entity_name' => \Application\Query\CreateFilter\EntityName::class, ], 'factories' => [ \Application\Query\CreateFilter\EntityName::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, ], ],
Register your Query Create Filter as:
'api-tools' => [ '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:
'api-tools' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\...Resource' => [ 'entity_factory' => 'key_in_service_manager', ... ], ], ],