Skip to content

Commit 9ccea9b

Browse files
authored
Merge pull request #771 from teohhanhui/fix/doctrine-orm-paginator
Rewrite the section on Doctrine ORM Paginator
2 parents fa6bfef + a4e8c66 commit 9ccea9b

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

core/pagination.md

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -320,31 +320,67 @@ class Book
320320
}
321321
```
322322

323-
## Avoiding double SQL requests on Doctrine ORM
323+
## Controlling the behavior of the Doctrine ORM Paginator
324324

325-
By default, the pagination assumes that there will be a collection fetched on a resource and thus will set `useFetchJoinCollection` to `true` on the Doctrine Paginator class. Having this option implies that 2 SQL requests will be executed (to avoid having less results than expected).
325+
The [PaginationExtension](https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php) of API Platform performs some checks on the `QueryBuilder` to guess, in most common cases, the correct values to use when configuring the Doctrine ORM Paginator:
326326

327-
In most cases, even without collection on the resource, this parameter has little impact on performance. However when fetching a lot of results per page it can be counter productive.
327+
- `$fetchJoinCollection` argument: Whether there is a join to a collection-valued association. When set to `true`, the Doctrine ORM Paginator will perform an additional query, in order to get the correct number of results.
328328

329-
That's why this behavior can be configured with the `pagination_fetch_join_collection` parameter on a resource:
329+
You can configure this using the `pagination_fetch_join_collection` attribute on a resource or on a per-operation basis:
330330

331-
```php
332-
<?php
333-
334-
// api/src/Entity/Book.php
331+
```php
332+
<?php
333+
// api/src/Entity/Book.php
335334
336-
use ApiPlatform\Core\Annotation\ApiResource;
335+
use ApiPlatform\Core\Annotation\ApiResource;
337336
338-
/**
339-
* @ApiResource(attributes={"pagination_fetch_join_collection"=false})
340-
*/
341-
class Book
342-
{
343-
// ...
344-
}
345-
```
337+
/**
338+
* @ApiResource(
339+
* attributes={"pagination_fetch_join_collection"=false},
340+
* collectionOperations={
341+
* "get",
342+
* "get_custom"={
343+
* ...
344+
* "pagination_fetch_join_collection"=true,
345+
* },
346+
* },
347+
* )
348+
*/
349+
class Book
350+
{
351+
// ...
352+
}
353+
```
354+
355+
- `setUseOutputWalkers` setter: Whether to use output walkers. When set to `true`, the Doctrine ORM Paginator will use output walkers, which are compulsory for some types of queries.
356+
357+
You can configure this using the `pagination_use_output_walkers` attribute on a resource or on a per-operation basis:
358+
359+
```php
360+
<?php
361+
// api/src/Entity/Book.php
362+
363+
use ApiPlatform\Core\Annotation\ApiResource;
364+
365+
/**
366+
* @ApiResource(
367+
* attributes={"pagination_use_output_walkers"=false},
368+
* collectionOperations={
369+
* "get",
370+
* "get_custom"={
371+
* ...
372+
* "pagination_use_output_walkers"=true,
373+
* },
374+
* },
375+
* )
376+
*/
377+
class Book
378+
{
379+
// ...
380+
}
381+
```
346382

347-
Please note that this parameter will always be forced to false when the resource has composite keys due to a [bug in Doctrine](https://github.com/doctrine/orm/issues/2910).
383+
For more information, please see the [Pagination](https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/pagination.html) entry in the Doctrine ORM documentation.
348384

349385
## Custom Controller Action
350386

0 commit comments

Comments
 (0)