-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: pagination support in JsonModel #12
Comments
@kukoman is this still an issue? Maybe your paginator and not the page_size was set to 10? Originally posted by @TomHAnderson at zfcampus/zf-content-negotiation#24 (comment) |
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 Originally posted by @kukoman at zfcampus/zf-content-negotiation#24 (comment) |
You could set it in the collection class. This is because the collection extends from Paginator class SomeCollection extends Paginator
{
protected static $defaultItemCountPerPage = 10;
} Originally posted by @agustincl at zfcampus/zf-content-negotiation#24 (comment) |
The root cause is because the The $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 Originally posted by @weierophinney at zfcampus/zf-content-negotiation#24 (comment) |
For those who still need pagination in JsonModel I 've realized a view strategy that follows the principles of the HalJson view strategy. The classes shown here are probably not the best way how to archive pagination attributes in the JsonModel / json content negotiation, but they work pretty well. The View StrategyFirst of all, a view strategy that only takes effect when it comes to our own view renderer, that will handle the pagination attributes.
The factory for the
The RendererAs you might have seen we need a renderer instance, that handles the pagination attributes. The renderer does basicly the same as the
As you can see the renderer has a dependency to a view helper and the
The View HelperThe renderer class uses a view helper, that renders a collection to a json string. Basicly this view helper does all the basic things, that the HAL view helper does without wiring links and handle single entites. This view helper class is just for rendering collections as JSON string. Beside that the attributes that we know from HAL are set. Theoretically, one could do without the event manager. However, since I need it in my application, it is included here.
All dependencies are done in the factory. Factories FTW!
ConfigurationAs @froschdesign said, the view strategy does not have to be hooked into the module class. It is sufficient to make the view strategy accessible in the configuration as we check for the right
That 's all. |
Register your strategy via the configuration and the module extension is not needed: 'view_manager' => [
'strategies' => [
MyViewStrategy::class,
],
], |
This is not possible, as long as the view strategy is only applicable to the |
See at your own strategy: use Laminas\ApiTools\ContentNegotiation\JsonModel;
// …
public function selectRenderer(ViewEvent $event)
{
$model = $event->getModel();
if (!$model instanceof JsonModel) {
return;
}
$this->renderer->setViewEvent($event);
return $this->renderer;
} |
@froschdesign |
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:
Originally posted by @kukoman at zfcampus/zf-content-negotiation#24
The text was updated successfully, but these errors were encountered: