jmsserializerbundle - JMS 序列化程序忽略 Knp 分页器的映射

标签 jmsserializerbundle knppaginator

我在使用 JMS Serializer 排除某些 KNP Paginator 属性时遇到问题。

首先,这包含在 composer.json 中

...
"jms/serializer-bundle": "~0.13",
"knplabs/knp-paginator-bundle": "2.4.*@dev",
...

我正在为 CrmContacts 实体分页,该实体的排除策略运行良好。我还为 KNP 分页器添加了 yml 文件,如下所示:

配置文件
jms_serializer:
    metadata:
        directories:
            KNPPB:
                namespace_prefix: 'Knp\\Bundle\\PaginatorBundle'
                path: %kernel.root_dir%/Resources/serializer/Knp

在 app/Resources/serializer/Knp 文件夹中,我创建了 Pagination.SlidingPagination.yml:
Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination:
    exclusion_policy: ALL
        properties:
            items:
                expose: true
                access_type: public_method
                accessor:
                    getter: getItems
                type: array
                serialized_name:
                    payload
            currentPageNumber:
                expose: true
                serialized_name:
                    page
            numItemsPerPage:
                expose: true
                serialized_name:
                    items
            totalCount:
                expose: true
                serialized_name:
                    totalItems

这是返回序列化数据的逻辑:
public function getContactsAction(Request $request)
{

    $limit = $request->query->getInt('l', 10);
    $page = $request->query->getInt('p', 1);

    $serializer = $this->get('jms_serializer');

    $contacts = $this->getDoctrine()
        ->getManager()
        ->getRepository('AcmeContactsBundle:CrmContact')
        ->getContacts();

    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $contacts,
        $page,
        $limit
    );

    return new Response(
        $serializer->serialize(
            $pagination,
            'json',
            SerializationContext::create()->setGroups(['Default'])
        ),
        Response::HTTP_OK,
        [
            'Content-Type' => 'application/json',
        ]
    );

}

不幸的是,作为回应,我从 Knp Paginator 获取所有属性:
{
    "currentPageNumber": 1,
    "numItemsPerPage": 10,
    "items": [
        {
            "id": 1,
            ...
        },
        {
            "id": 2,
            ...
        },
        {
            "id": 3,
            ...
        }
    ],
    "totalCount": 3,
    "paginatorOptions": {
        "pageParameterName": "page",
        "sortFieldParameterName": "sort",
        "sortDirectionParameterName": "direction",
        "filterFieldParameterName": "filterField",
        "filterValueParameterName": "filterValue",
        "distinct": true
    },
    "customParameters": [],
    "route": "acmeContactsGetContacts",
    "params": [],
    "pageRange": 5,
    "template": "KnpPaginatorBundle:Pagination:sliding.html.twig",
    "sortableTemplate": "KnpPaginatorBundle:Pagination:sortable_link.html.twig",
    "filtrationTemplate": "KnpPaginatorBundle:Pagination:filtration.html.twig"
}

最佳答案

您要映射的属性归 Knp\Component\Pager\Pagination\AbstractPagination 所有。

您还想隐藏其余的属性,因此您必须配置这两个类。

我刚刚尝试了以下方法,它对我有用。

应用程序/配置/config.yml

jms_serializer:
metadata:
    directories:
        KnpPaginatorBundle:
            namespace_prefix: Knp\Bundle\PaginatorBundle
            path: %kernel.root_dir%/config/serializer/KnpPaginatorBundle
        KnpPager:
            namespace_prefix: Knp\Component\Pager
            path: %kernel.root_dir%/config/serializer/KnpPager

app/config/serializer/KnpPager/Pagination.AbstractPagination.yml
Knp\Component\Pager\Pagination\AbstractPagination:
exclusion_policy: ALL
properties:
    items:
        expose: true
        access_type: public_method
        accessor:
            getter: getItems
        type: array
        serialized_name:
            payload
    currentPageNumber:
        expose: true
        serialized_name:
            page
    numItemsPerPage:
        expose: true
        serialized_name:
            items
    totalCount:
        expose: true
        serialized_name:
            totalItems

app/config/serializer/KnpPaginatorBundle/Pagination.SlidingPagination.yml
Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination:
exclusion_policy: ALL

不要忘记在测试前清除缓存。

希望这对你有帮助。

关于jmsserializerbundle - JMS 序列化程序忽略 Knp 分页器的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214339/

相关文章:

jms - FOSRestbundle、JMS Serializer 和 SonataMediaBundle 返回图像的公共(public) url

symfony - 在symfony 2.7中找不到'templating.helper.router'服务

mysql - Symfony2 Doctrine 错误 : Cannot count query that uses a HAVING clause. 使用输出 walkers 进行分页

php - 添加数据到分页器(KnpPaginatorBundle,Symfony)

symfony - 听众之一必须对给定的目标进行计数和切片

php - JMSSerializerBundle,反序列化后持久化没有关系

symfony - 在 Symfony2 中运行 PHPUnit 测试时,JMS Serializer 不读取配置

php - 如何在 Symfony3 中初始化 JMS 序列化程序?

mysql - 使用嵌套类别反序列化 JSON

php - Symfony2 KNP 分页 "Cannot count query "