Skip to content

Commit

Permalink
Paginator shim (#110)
Browse files Browse the repository at this point in the history
* Paginator shim
  • Loading branch information
dereuromark committed Nov 1, 2023
1 parent cf1a83e commit ffd0b7e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/Datasource/NumericPaginator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# NumericPaginator

`contain` is deprecated/removed in 5.x as per https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#controller

For those with hundreds of controllers using this option it can be tedious to upgrade all at once.
In that case one can use this one instead:

```php
use Shim\Datasource\Paging\NumericPaginator;

/**
* @var array<string, mixed>
*/
protected array $paginate = [
'order' => ['States.modified' => 'DESC'],
'className' => NumericPaginator::class,
];
```
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ Database

Datasource
- [LegacyModelAwareTrait](Datasource/LegacyModelAwareTrait.md)
- [NumericPaginator](Datasource/NumericPaginator.md)

Model
- Model
- [Table shim](Model/Table.md)
- [Entity Get/Fail](Model/Entity.md)
- [Nullable behavior](Model/Nullable.md)
Expand Down
36 changes: 36 additions & 0 deletions src/Datasource/Paging/NumericPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Shim\Datasource\Paging;

use Cake\Datasource\Paging\NumericPaginator as CoreNumericPaginator;
use Cake\Datasource\QueryInterface;
use Cake\Datasource\RepositoryInterface;

class NumericPaginator extends CoreNumericPaginator {

/**
* Get query for fetching paginated results.
*
* @param \Cake\Datasource\RepositoryInterface $object Repository instance.
* @param \Cake\Datasource\QueryInterface|null $query Query Instance.
* @param array<string, mixed> $data Pagination data.
* @return \Cake\Datasource\QueryInterface
*/
protected function getQuery(RepositoryInterface $object, ?QueryInterface $query, array $data): QueryInterface {
$contain = null;
if (!empty($data['options']['contain'])) {
$contain = $data['options']['contain'];
}

unset($data['options']['contain']);

$query = parent::getQuery($object, $query, $data);
if ($contain) {
/** @var \Cake\ORM\Query\SelectQuery $query */
$query->contain($contain);
}

return $query;
}

}

0 comments on commit ffd0b7e

Please sign in to comment.