Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Feature request: pagination support in JsonModel #24

Open
kukoman opened this issue Oct 21, 2014 · 5 comments
Open

Feature request: pagination support in JsonModel #24

kukoman opened this issue Oct 21, 2014 · 5 comments

Comments

@kukoman
Copy link

kukoman commented Oct 21, 2014

Hi;
if i use "Content Negotiation Selector": Json; the paginator is not working correctly and
i always get 10 rows in collection, if i change it to HalJson it works as expecting

example:

'zf-rest' => [
   'service\\name\\...' => [
   'page_size' => 25, // with JSON it is always 10, no matter if i set it to -1 or 1000
// ...
]
@TomHAnderson
Copy link
Contributor

@kukoman is this still an issue? Maybe your paginator and not the page_size was set to 10?

@kukoman
Copy link
Author

kukoman commented Dec 10, 2014

yep, still the same behavior

to better explain whats going on:

// my EquipmentResource::fetchAll method
    public function fetchAll($params = array())
    {
        $adapter = new ArrayAdapter($this->getEquipmentService()->fetchAll($params));
        $collection = new EquipmentCollection($adapter);
        return $collection;
   }

// zf-rest config:

        'Mcm\\V1\\Rest\\Equipment\\Controller' => array(
            'listener' => 'Mcm\\V1\\Rest\\Equipment\\EquipmentResource',
            'route_name' => 'mcm.rest.equipment',
            'route_identifier_name' => 'equipment_id',
            'collection_name' => 'equipment',
            'entity_http_methods' => array(
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ),
            'collection_http_methods' => array(
                0 => 'GET',
                1 => 'POST',
            ),
            'collection_query_whitelist' => array(
                0 => 'orderBy',
                1 => 'query',
                2 => 'filter',
            ),
            'page_size' => 100,
            'page_size_param' => 100,
            'entity_class' => 'Mcm\\V1\\Rest\\Equipment\\EquipmentEntity',
            'collection_class' => 'Mcm\\V1\\Rest\\Equipment\\EquipmentCollection',
            'service_name' => 'Equipment',
        ),

and still you get only 10 results

if I change it from JSON to HAL it works as expected

@agustincl
Copy link

agustincl commented Jun 18, 2015

You could set it in the collection class. This is because the collection extends from Paginator

class SomeCollection extends Paginator
{
    protected static $defaultItemCountPerPage = 10;
}

@weierophinney
Copy link
Member

The root cause is because the JsonModel does not do pagination; all it does is serialize the value presented using json_encode(). When presented with any iterator, json_encode() returns an object, with the keys being the indexes, and the value at that index of the iterator. This is true with paginators as well.

The page_size and page_size_param values are only used by zf-hal. As such, you will need to update your controller to inject the page and page size prior to returning the paginator:

$collection->setCurrentPageNumber($request->getQuery('page', 1));
$collection->setItemCountPerPage(25); // you might want to inject this value from configuration

We may add pagination support to zf-content-negotiation's JsonModel in the future, but for now, the above is how to handle it.

@weierophinney weierophinney changed the title bug in paginator Feature request: pagination support in JsonModel Jul 7, 2016
@weierophinney
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools-content-negotiation; a new issue has been opened at laminas-api-tools/api-tools-content-negotiation#12.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants