Conversation
GromNaN
left a comment
There was a problem hiding this comment.
You decided not using the $facet stage, as done in Api Platform.
| { | ||
| $builder = clone $this->aggregation; | ||
| $results = $builder | ||
| ->hydrate(null) |
There was a problem hiding this comment.
What's the difference between hydrate(null) and hydrate(false) used in the query paginators?
There was a problem hiding this comment.
The type, probably nothing else. We should eventually have a string|false type to control this.
| return (int) ceil($builder | ||
| ->hydrate(false) | ||
| ->count() | ||
| ->getQuery() | ||
| ->execute() / $this->perPage); |
There was a problem hiding this comment.
This is immediately forgetting the exact number of pages. I think it would be better to make this value accessible with a method such as getNumberOfResults().
There was a problem hiding this comment.
We can add something like this. Since we're discussing this, how do you feel about the paginator count being the number of pages? I felt that made more sense than returning the number of results (or worse, the number of items on the current page). If we don't want to change the Countable behaviour, I would use something like countResults (or similar) and use that to return the number of results, storing them in a private property as well.
Summary
This PR introduces four new paginator classes. We introduce two styles of paginator: offset-based and cursor-based. Both paginator styles come in flavours for aggregation pipeline and queries.
Offset-based pagination
Offset-based paginators take a page number and number of items per page (defaults to 24), along with a builder for aggregation or queries. It is
Countableand returns the number of pages when counted. It provides an iterator for the current page. To accomplish this, it uses thecountoperation for queries, and$numDocumentsfor aggregation pipeline. When fetching items, it inserts the correctskipandlimitoptions/stages.Cursor-based pagination
Cursor-based pagination takes an ID where to continue paginating, the number of items per page, and an optional field name for the cursoring field (an
ObjectIdvalue in the_idfield is usually a good choice). Together with the builder for aggregation or queries you can iterate over the items of the current "page". Iterating the paginator multiple times always returns the same items. You are responsible for extracting the ID of the last item, which is used when creating the paginator for the next page.Todo