-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
Support offset/slice in pagination #4672
Comments
@dunglas , what do you think of a PR that adds "offset" (or "starting_at") to the arguments, then Currently the pagination extension that returns a slice of the results given page_number and records_per_page, e.g. page 3, 50 records, getPagination() return [100, 50] // vendor/api-platform/core/src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php:121
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{
if (null === $pagination = $this->getPagination($queryBuilder, $resourceClass, $operationName, $context)) {
return;
}
[$offset, $limit] = $pagination;
$queryBuilder
->setFirstResult($offset)
->setMaxResults($limit);
} What I want is just a slice, not really a paginator. I'm not sure if the best approach is to override the paginator, or create a custom data collector, but what about building it the paginator? ['arg_name' => 'offset', 'type' => 'int', 'default' => 0],
['arg_name' => 'offsetParameterName', 'type' => 'string', 'default' => 'offset'],
['arg_name' => 'enabledOffset', 'type' => 'boolean', 'default' => false], Then if offset were enabled, the paginator would return the values passed in and not need to calculate the offset / limit as it does now given the page number. public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{
// if offset it enabled, use it, otherwise call the paginator..
[$offset, $limit] = [41, 220];
$queryBuilder
->setFirstResult($offset)
->setMaxResults($limit);
} I've asked this on stackoverflow, too. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@tacman plz I need the same aspect, the param was added or not ? |
No, and I don't know how to override it. @dunglas , would you accept a PR if this were added? Or provide direction in how to implement it with a custom paginator? |
Hello!
3- decorate OpenApiFactory to add limit and offset for params in query for every collection operation.
And do not forget to add the extension in the service config
Hope it helps. |
Description
I would like to use API Platform to get a slice of the results, not a page.
Example
/api/...?offset=422&items_per_page=81
A javascript library I'm using for infinite scrolling fetches more than a "page". Currently within API Platform, the offset is calculated from the page and returned, I'd like to override what's returned.
I considered extending the Doctrine ORM paginator class, but it's marked as final.
There's probably an elegant solution here somewhere, but I can't find it, so maybe it can be added. What I want is identical to the Doctrine ORM provider with pagination, so ideally I'd like to leverage the existing code and simply change the first result offset.
The text was updated successfully, but these errors were encountered: