JMS Serializer忽略Knp Paginator的映射

nbu*_*cic 10 jmsserializerbundle knppaginator

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

首先,它包含在composer.json中

...
"jms/serializer-bundle": "~0.13",
"knplabs/knp-paginator-bundle": "2.4.*@dev",
...
Run Code Online (Sandbox Code Playgroud)

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

config.yml

jms_serializer:
    metadata:
        directories:
            KNPPB:
                namespace_prefix: 'Knp\\Bundle\\PaginatorBundle'
                path: %kernel.root_dir%/Resources/serializer/Knp
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

这是返回序列化数据的逻辑:

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',
        ]
    );

}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我收到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"
}
Run Code Online (Sandbox Code Playgroud)

小智 19

要映射的属性由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
Run Code Online (Sandbox Code Playgroud)

应用程序/配置/串行器/ 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
Run Code Online (Sandbox Code Playgroud)

应用程序/配置/串行器/ KnpPaginatorBundle/Pagination.SlidingPagination.yml

Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination:
exclusion_policy: ALL
Run Code Online (Sandbox Code Playgroud)

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

希望这对你有所帮助.