### Install api-tools-doctrine with Composer Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Installs the api-tools-doctrine module and its dependencies using Composer. Ensure you have Composer installed and configured. ```bash composer require laminas-api-tools/api-tools-doctrine ``` -------------------------------- ### Configure Doctrine Connected Resources with Query Create Filter Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md This configuration example demonstrates how to link a resource (e.g., Api\V1\Rest\....) to a specific query create filter ('entity_name') within the 'doctrine-connected' section of the 'api-tools' configuration. ```php 'api-tools' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\....' => [ 'query_create_filter' => 'entity_name', ... ], ], ], ``` -------------------------------- ### Using Multiple Keys as Entity Identifier in API Calls Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/docs/RELEASE-0.3.3.md This example shows how to define and use multiple fields as an entity identifier by leveraging a delimiter (defaulting to '.'). This allows for API calls to identify resources using combinations of fields that may not be direct entity identifiers. ```php 'entity_identifer_name' => 'email.shop' ``` -------------------------------- ### Doctrine Resource Creation API Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Used to create Doctrine-based API resources. This endpoint allows for the configuration of various aspects of the resource, including the object manager, service name, entity class, and routing. ```APIDOC ## POST /api-tools/api/module[/:name]/doctrine[/:controller_service_name] ### Description Creates a Doctrine resource route, similar to Laminas API Tools' REST routes. To create a resource, do not include `[/:controller_service_name]` in the endpoint path. This endpoint is used to configure and register new Doctrine-based API resources. ### Method POST ### Endpoint /api-tools/api/module[/:name]/doctrine[/:controller_service_name] #### Path Parameters - **name** (string) - Required - The name of the module where the resource will be created. - **controller_service_name** (string) - Optional - The service name for the resource controller. If not provided, it will be generated. #### Request Body - **objectManager** (string) - Required - The Doctrine object manager to use (e.g., "doctrine.entitymanager.orm_default"). - **serviceName** (string) - Required - The name of the service for the resource (e.g., "Artist"). - **entityClass** (string) - Required - The fully qualified class name of the entity (e.g., "Db\Entity\Artist"). - **routeIdentifierName** (string) - Required - The name of the route identifier parameter (e.g., "artist_id"). - **entityIdentifierName** (string) - Required - The name of the entity's identifier field (e.g., "id"). - **routeMatch** (string) - Required - The base route for the resource (e.g., "/api/artist"). - **pageSizeParam** (string) - Optional - The query parameter name for page size (default is null). - **hydratorName** (string) - Optional - The name of the hydrator to use for the entity (default is generated). ### Request Example ```json { "objectManager": "doctrine.entitymanager.orm_default", "serviceName": "Artist", "entityClass": "Db\Entity\Artist", "routeIdentifierName": "artist_id", "entityIdentifierName": "id", "routeMatch": "/api/artist", "pageSizeParam": "limit", "hydratorName": "DbApi\V1\Rest\Artist\ArtistHydrator" } ``` ``` -------------------------------- ### Register Custom Doctrine Query Providers in PHP Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Illustrates the configuration for registering custom Doctrine query providers using a plugin manager. This enables custom query logic for various Doctrine operations like fetching entities. ```php '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, ], ], ``` -------------------------------- ### Register Custom Doctrine Create Filters in PHP Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Shows the configuration pattern for registering custom create filters with Doctrine. This allows pre-processing of data before it's used to hydrate an entity during creation. ```php 'api-tools-doctrine-query-create-filter' => [ 'aliases' => [ ``` -------------------------------- ### Configure Resource with Custom Event Listeners in PHP Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Shows how to configure a specific Doctrine-connected resource to use custom event listeners registered in the service manager. This allows for resource-specific event handling. ```php 'api-tools' => [ 'doctrine-connected' => [ 'Api\V1\Rest\User\UserResource' => [ // ... 'listeners' => [ 'key.of.aggregate.listener.in.service_manager', ], ], ], ], ``` -------------------------------- ### Assign Query Providers to Doctrine Resource in PHP Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Demonstrates how to assign specific query providers to a Doctrine-connected resource through configuration. This allows overriding default query behavior for operations like fetching all entities. ```php 'api-tools' => [ 'doctrine-connected' => [ 'Api\V1\Rest\....' => [ 'query_providers' => [ 'default' => 'default_orm', 'fetch_all' => 'entity_name_fetch_all', // or fetch, update, patch, delete ], ], ], ], ``` -------------------------------- ### Register EntityName Query Create Filter Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md This snippet shows how to register an EntityName query create filter within the Laminas Service Manager configuration. It specifies the class name for the filter and its factory. ```php 'entity_name' => \Application\Query\CreateFilter\EntityName::class, ], 'factories' => [ \Application\Query\CreateFilter\EntityName::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, ], ], ``` -------------------------------- ### Configure Query Providers in api-tools-doctrine Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/docs/UPGRADE-0.3.0.md This snippet shows how to configure default and custom query providers within the api-tools-doctrine configuration. It specifies the mapping between query provider names and their respective implementations. ```php 'query_providers' => array( 'default' => 'default_odm', 'fetch_all' => 'Key.in.api-tools-doctrine-query-provider', ), ``` -------------------------------- ### Attach Custom Event Listener in PHP Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Demonstrates how to attach a custom listener to a Doctrine resource event using the Shared Event Manager. This allows intercepting and modifying entity operations like creation. ```php 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'); } ); ``` -------------------------------- ### Configure Entity Factory for Doctrine Entities Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md This snippet illustrates how to specify a custom entity factory for Doctrine entities within the 'doctrine-connected' configuration. By setting 'entity_factory' to a key registered in the Service Manager, you can delegate entity instantiation, useful for entities with complex constructor requirements. ```php 'api-tools' => [ 'doctrine-connected' => [ 'Api\\V1\\Rest\\...Resource' => [ 'entity_factory' => 'key_in_service_manager', ... ], ], ], ``` -------------------------------- ### Define Doctrine API Resource Route Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Manually defines a Doctrine resource route for Laminas API Tools. This route structure is similar to the default REST routes but specifically targets Doctrine entities. It specifies parameters like object manager alias, service name, entity class, and route identifiers. ```json { "objectManager": "doctrine.entitymanager.orm_default", "serviceName": "Artist", "entityClass": "Db\\Entity\\Artist", "routeIdentifierName": "artist_id", "entityIdentifierName": "id", "routeMatch": "/api/artist", "pageSizeParam": "limit", "hydratorName": "DbApi\\V1\\Rest\\Artist\\ArtistHydrator" } ``` -------------------------------- ### Doctrine Metadata API Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/README.md Retrieves metadata for Doctrine entities. You can query for a specific entity's metadata by providing its name, or retrieve all metadata for a given object manager by omitting the entity name. ```APIDOC ## GET /api-tools/api/doctrine[/:object_manager_alias]/metadata[/:name] ### Description Retrieves metadata for the named Doctrine entity, which is part of the specified object manager. If no entity name is provided, it returns all metadata for the object manager. ### Method GET ### Endpoint /api-tools/api/doctrine[/:object_manager_alias]/metadata[/:name] #### Path Parameters - **object_manager_alias** (string) - Optional - The alias of the Doctrine object manager. - **name** (string) - Optional - The name of the entity for which to retrieve metadata. ### Response #### Success Response (200) - **metadata** (object) - An object containing metadata for the requested entity or entities. ``` -------------------------------- ### Validate OAuth2 Client Scope and Inject User Identity Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/docs/RELEASE-0.3.2.md This PHP code defines a Query Create Filter for api-tools-doctrine. It validates if an authenticated OAuth2 client possesses the 'create' scope and injects the user's ID into the entity's data. It relies on LaminasApiToolsDoctrineServerQueryCreateFilterDefaultCreateFilter and requires an OAuth2 Server instance. ```php namespace RollNApi\Query\CreateFilter; use Laminas\ApiTools\Doctrine\Server\Query\CreateFilter\DefaultCreateFilter; use Laminas\ServiceManager\ServiceLocatorAwareInterface; use Laminas\ServiceManager\ServiceLocatorInterface; use Laminas\ApiTools\ApiProblem\ApiProblem; use Laminas\ApiTools\Rest\ResourceEvent; class UserAlbumCreateFilter extends DefaultCreateFilter { public function filter(ResourceEvent $event, $entityClass, $data) { $validate = $this->validateOAuth2('create'); if ($validate instanceof ApiProblem) { return $validate; } $request = $event->getRequest()->getQuery()->toArray(); $identity = $event->getIdentity()->getAuthenticationIdentity(); $data->user = $identity['user_id']; return $data; } } ``` -------------------------------- ### Configure Doctrine Entity Identifier Name in api-tools-rest Source: https://github.com/laminas-api-tools/api-tools-doctrine/blob/2.5.x/docs/RELEASE-0.3.3.md This configuration snippet demonstrates how to set the 'entity_identifier_name' within the 'api-tools-rest' configuration. This allows specifying which field from the entity metadata should be used as the identifier when interacting with the API. ```php 'api-tools-rest' => array( 'LaminasTestApiToolsDbApi\\V1\\Rest\\ArtistByName\\Controller' => array( 'listener' => 'LaminasTestApiToolsDbApi\\V1\\Rest\\ArtistByName\\ArtistByNameResource', 'route_name' => 'api-tools-test-api-tools-db-api.rest.doctrine.artist-by-name', 'route_identifier_name' => 'artist_name', 'entity_identifier_name' => 'name', 'collection_name' => 'artist_by_name', 'entity_http_methods' => array( ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.