2014-11-30 25 views
7

Mam problem z wyłączeniem niektórych właściwości Paginator KNP z JMS Serializer.Serializer JMS ignoruje odwzorowania dla Knp Paginator

Po pierwsze, jest to zawarte w composer.json

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

mam Stronicowanie podmiot CrmContacts i polityki wykluczania dla że jednostka działa dobrze. Dodałem też plik yml dla KNP paginator tak:

config.yml

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

wewnątrz serializer folderze app/Resources// KNP Utworzyłem 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 

i to jest logika za zwrócenie danych odcinkach:

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

} 

Niestety dostaję wszystkie pr operties z KNP paginator w odpowiedzi:

{ 
    "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" 
} 

Odpowiedz

16

właściwości, które chcesz mapować są własnością KNP \ Komponent \ pager paginacja \ \ AbstractPagination.

Chcesz ukryć pozostałe właściwości, więc będziesz musiał skonfigurować obie klasy.

Właśnie próbowałem następujących i to działa dla mnie.


APP/konfiguracji/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/konfiguracji/serializacji/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/konfiguracji/serializacji/KnpPaginatorBundle/Stronicowanie .SlidingPagination.yml

Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination: 
exclusion_policy: ALL 

Nie zapomnij wyczyścić pamięci podręcznej przed testowaniem.

Mam nadzieję, że to pomoże.

3

Zamiast szeregowania cały obiekt paginacji, spróbuj serializacji tylko dane i przedmiotów, takich jak to:

$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',] 
); 
Powiązane problemy