The hydrator settings are ignored in Apigility

让人想犯罪 __ 提交于 2019-12-04 13:20:11

When you defined the Entity class for your service, you also have the option to configure the Collection entity for your service. You need to to associate your hydrator to the Collection entity in your metadata.

return [
    'zf-rest' => [
        'Portfolio\\V2\\Rest\\Project\\Controller' => [
            // other config
            'entity_class' => 'Portfolio\\V2\\Rest\\Project\\ProjectEntity',
            'collection_class' => 'Portfolio\\V2\\Rest\\Project\\ProjectCollection',
        ]
    ],
    'zf-hal' => [
        'metadata_map' => [
            'Portfolio\\V2\\Rest\\Project\\ProjectEntity' => [
                // other config
                'hydrator' => 'MyNamespace\\Hydrator\\ProjectHydrator'
            ],
            'Portfolio\\V2\\Rest\\Project\\ProjectCollection' => [
                // other config
                'hydrator' => 'MyNamespace\\Hydrator\\ProjectCollectionHydrator'
            ]
        ]
    ]
]

Old but found this: For collectionRender the hydrator is not called https://github.com/zfcampus/zf-hal/blob/master/src/Plugin/Hal.php#L570

For entityRender is: https://github.com/zfcampus/zf-hal/blob/master/src/Plugin/Hal.php#L695

Need to call issue for that.

Edit[07.10.16]:

I was wrong with that.

My solution works with Apigility 1.3. Didn't test earlier versions.

You can use Zend\\Hydrator\\DelegatingHydrator

        'metadata_map' => array(
        'Test\\DomainModel\\UseCase\\Query\\ShowTask\\Response\\Task' => array(
            'entity_identifier_name' => 'uuid',
            'route_name' => 'descriptive-module.rest.task',
            'route_identifier_name' => 'task_id',
            'hydrator' => 'Zend\\Hydrator\\DelegatingHydrator',
        ),
        'Test\\DomainModel\\UseCase\\Query\\ListTask\\Response\\Task' => array(
            'entity_identifier_name' => 'uuid',
            'route_name' => 'descriptive-module.rest.task',
            'route_identifier_name' => 'task_id',
            'hydrator' => 'Zend\\Hydrator\\DelegatingHydrator',
        ),

First one, for entity. Second one, elements of returned Collection.

Then in module.config.php

   'hydrators' => array(
    'invokables' => array(
        \Test\DomainModel\UseCase\Query\ShowTask\Response\Task::class =>
            \TestApi\V1\Rest\Task\Hydrator\ShowTaskHydrator::class,
        \Test\DomainModel\UseCase\Query\ListTask\Response\Task::class =>
            \TestApi\V1\Rest\Task\Hydrator\ListTaskHydrator::class,
    ),
    'factories' => array(),
),

TaskShowHydrator/ListTaskHydrator need implement Zend\Hydrator\HydratorInterface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!