JMS Serializer ignores mappings for Knp Paginator

荒凉一梦 提交于 2019-12-03 06:11:38
Juan Carlos Ruiz

The properties that you want to map are owned by Knp\Component\Pager\Pagination\AbstractPagination.

You also want to hide the rest of properties, so you will have to configure both classes.

I've just tried the following and it's working for me.


app/config/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

Don't forget to clear the cache before testing.

Hope this helps you.

Instead of serializing all the pagination object, try to serialize only the data and items, like this:

$result = array(
  'data' => $pagination->getItems(),
  'meta' => $pagination->getPaginationData());

return new Response(
    $serializer->serialize(
        $result,
        'json',
        SerializationContext::create()->setGroups(['Default'])
    ),
    Response::HTTP_OK,
    ['Content-Type' => 'application/json',]
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!