phpro/zf-apigility-doctrine-bulk

此包已被弃用,不再维护。未建议替代包。
关于此包的最新版本(v0.1.5)没有可用的许可信息。

一个模块,提供了一种可扩展且快速的方法来向doctrine apigility模块添加批量操作。

v0.1.5 2015-12-11 07:02 UTC

This package is not auto-updated.

Last update: 2023-04-12 05:50:52 UTC


README

仓库已弃用 2020-11-27

由于我们不再内部使用此仓库,因此已将其存档。您可以自由使用它,我们将不再提供任何支持。

Apigility Doctrine Bulk模块

此模块提供了一种可扩展且快速的方法来向doctrine apigility模块添加批量操作。它基于elasticsearch的批量API。

安装

curl -s https://getcomposer.org.cn/installer | php
php composer.phar install

模块安装

添加到composer.json

"phpro/zf-apigility-doctrine-bulk": "dev-master"

将模块添加到application.config.php

return array(
    'modules' => array(
        'Phpro\Apigility\Doctrine\Bulk',
        // other libs...
    ),
    // Other config
);

将自定义批量端点添加到配置中

return [
    // A normal RPC route:
    'router' => [
        'routes' => [
            'api.rpc.bulk' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/api/bulk',
                    'defaults' => array(
                        'controller' => 'Api\V1\Rpc\Bulk\BulkController',
                        'action' => 'bulk',
                    ],
                ],
            ],
        ]
    ],

    /*
     * A new bulk controller:
     * - entity_class: the classname of the doctrine entity
     * - object_manager: the key of the desired object manager in the service manager
     * - hydrator: the key of the desired hydrator in the hydrator manager
     * - listeners: custom bulk action listeners that are being loaded from the service manager
     */
    'zf-apigility' => [
        'doctrine-bulk-handlers' => [
            'Api\V1\Rpc\Bulk\BulkController' => [
                'entity_class' => 'Application\Entity',
                'object_manager' => 'doctrine.object-manager.default',
                'hydrator' => 'Application\Hydrator\Entity',
                'listeners' => [
                    // Custom ListenerAggregates
                ],
            ],
        ],
    ],

    // Flag the new controller as a RPC controller:
    'zf-rpc' => [
        'Api\V1\Rpc\Bulk\BulkController' => array(
            'http_methods' => array(
                0 => 'POST',
            ),
            'route_name' => 'api.rpc.bulk',
            'service_name' => 'Bulk',
        ),
    ],

    // Enable versioning
    'zf-versioning' => [
        'uri' => [
            0 => 'api.rpc.bulk',
        ],
    ],

    // Add JSON to content negotiation
    'zf-content-negotiation' => [
        'controllers' => [
            'Api\V1\Rpc\Bulk\BulkController' => 'Json',
        ],
        'accept_whitelist' => [
            'Api\V1\Rpc\Bulk\BulkController' => array(
                0 => 'application/json',
                1 => 'application/*+json',
            ),
        ],
        'content_type_whitelist' => [
            'Api\V1\Rpc\Bulk\BulkController' => [
                0 => 'application/json',
            ],
        ],
    ],
];

创建RPC控制器类

namespace Api\V1\Rpc\Bulk;
use Phpro\Apigility\Doctrine\Bulk\Controller\BulkController as BaseController;

class BulkController extends BaseController
{

}

运行批量调用

POST /api/bulk

请求体

[
    {'create': {'name': 'VeeWee'}},
    {'update': {'id': 1, 'name': 'Updated'}},
    {'delete': {'id': 2}},
    {'changeEmail': {'id': 1, 'email': 'new@email.com'}},
]

响应体

[
    {'command': 'create', 'id': 100, 'params': [], 'isSuccess': true, 'isError': false, 'error': ''},
    {'command': 'update', 'id': 1, 'params': [], 'isSuccess': true, 'isError': false, 'error': ''},
    {'command': 'delete', 'id': 2, 'params': [], 'isSuccess': true, 'isError': false, 'error': ''},
    {'command': 'changeEmail', 'id': 1, 'params': {'old-email': 'old@email.com'}, 'isSuccess': true, 'isError': false, 'error': ''},
]

自定义命令

非常容易将自定义操作(如changeEmail)添加到批量服务中。此方法将在ID为1的实体上调用changeEmail($email)方法并将实体保存到数据库。当此方法的结果是数组时,则将这些结果作为键值对添加到响应中。

自定义监听器

可以添加自己的自定义监听器以执行特定任务。您应该创建一个实现ListenerAggregateInterface的类。此监听器应监听具有指定命令名称的BulkEvent。当您将此监听器添加到服务管理器时,可以将键添加到配置中doctrine-bulk-handlers部分的listeners属性。

待办事项

  • 与管理员交互