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

Commit 5b52367

Browse files
committed
Merge branch 'hotfix/fix-pagination-offset-calculation'
Close #56
2 parents fbd05cf + 0d3c83f commit 5b52367

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file, in reverse
44

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

7-
## 1.3.1 - TBD
7+
## 1.3.1 - 2019-02-11
88

99
### Added
1010

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

2525
### Fixed
2626

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

2929
## 1.3.0 - 2019-02-06
3030

docs/book/doctrine.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ seed a collection paginator (in the case of the `ListAlbumsHandler`). These
365365
values are known by the metadata map, and, as such, we can generate HAL
366366
resources for them without needing any other information.
367367

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

370380
Sometimes we will want to return an entire collection at once. The `getResult()`

src/ResourceGenerator/ExtractCollectionTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ private function extractDoctrinePaginator(
118118
return $this->createPaginatedCollectionResource(
119119
$pageCount,
120120
$data,
121-
function (int $page) use ($query, $pageCount) {
122-
$query->setFirstResult($pageCount * ($page - 1));
121+
function (int $page) use ($query, $perPage) {
122+
$query->setFirstResult($perPage * ($page - 1));
123123
},
124124
$collection,
125125
$metadata,

test/ResourceGenerator/DoctrinePaginatorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public function testCreatesLinksForQueryBasedPagination()
153153
->method('getMaxResults')
154154
->with()
155155
->willReturn(15);
156+
$query->expects($this->once())
157+
->method('setFirstResult')
158+
->with(30);
156159
$this->paginator->getQuery()->willReturn($query);
157160
$this->paginator->count()->willReturn(100);
158161

@@ -218,6 +221,9 @@ public function testCreatesLinksForRouteBasedPagination()
218221
->method('getMaxResults')
219222
->with()
220223
->willReturn(15);
224+
$query->expects($this->once())
225+
->method('setFirstResult')
226+
->with(30);
221227
$this->paginator->getQuery()->willReturn($query);
222228
$this->paginator->count()->willReturn(100);
223229

0 commit comments

Comments
 (0)