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

Commit

Permalink
Merge branch 'hotfix/fix-pagination-offset-calculation'
Browse files Browse the repository at this point in the history
Close #56
  • Loading branch information
weierophinney committed Feb 11, 2019
2 parents fbd05cf + 0d3c83f commit 5b52367
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file, in reverse

Versions prior to 0.4.0 were released as the package "weierophinney/hal".

## 1.3.1 - TBD
## 1.3.1 - 2019-02-11

### Added

Expand All @@ -24,7 +24,7 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal".

### Fixed

- Nothing.
- [#56](https://github.com/zendframework/zend-expressive-hal/pull/56) fixes an issue calculating the offset when generating a paginated Doctrine collection.

## 1.3.0 - 2019-02-06

Expand Down
10 changes: 10 additions & 0 deletions docs/book/doctrine.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ seed a collection paginator (in the case of the `ListAlbumsHandler`). These
values are known by the metadata map, and, as such, we can generate HAL
resources for them without needing any other information.

> ### Setting the offset
>
> When you plan to use paginated Doctrine result sets, you DO NOT need to
> call `$query->setFirstResult()`. This will be called when generating the
> result set based on the current page and the value of
> `$query->getMaxResults()`.
>
> You MUST call `$query->setMaxResults()` prior to generating your resource if
> you want it to be paginated, however.
## Example: Doctrine Collections

Sometimes we will want to return an entire collection at once. The `getResult()`
Expand Down
4 changes: 2 additions & 2 deletions src/ResourceGenerator/ExtractCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ private function extractDoctrinePaginator(
return $this->createPaginatedCollectionResource(
$pageCount,
$data,
function (int $page) use ($query, $pageCount) {
$query->setFirstResult($pageCount * ($page - 1));
function (int $page) use ($query, $perPage) {
$query->setFirstResult($perPage * ($page - 1));
},
$collection,
$metadata,
Expand Down
6 changes: 6 additions & 0 deletions test/ResourceGenerator/DoctrinePaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ public function testCreatesLinksForQueryBasedPagination()
->method('getMaxResults')
->with()
->willReturn(15);
$query->expects($this->once())
->method('setFirstResult')
->with(30);
$this->paginator->getQuery()->willReturn($query);
$this->paginator->count()->willReturn(100);

Expand Down Expand Up @@ -218,6 +221,9 @@ public function testCreatesLinksForRouteBasedPagination()
->method('getMaxResults')
->with()
->willReturn(15);
$query->expects($this->once())
->method('setFirstResult')
->with(30);
$this->paginator->getQuery()->willReturn($query);
$this->paginator->count()->willReturn(100);

Expand Down

0 comments on commit 5b52367

Please sign in to comment.