Skip to content

Commit

Permalink
Shim conditions/contain
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark authored Sep 4, 2024
1 parent b0437e0 commit c68f53f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/Controller/Controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,52 @@ shims it for older (IE) version to work there, as well.
### Response headers
Using `Configure::write('Shim.monitorHeaders')` you can monitor if all headers are properly
set via Response class and not for some reason sent prior to that.

## Pagination conditions/contain
If you have a large app with many controllers in the old way, rewriting them can be cumbersome.
You can also use this method as override in your AppController to shim it for now:
```php
public function paginate(
RepositoryInterface|QueryInterface|string|null $object = null,
array $settings = [],
): PaginatedInterface {
if (!is_object($object)) {
$object = $this->fetchTable($object);
}

$defaults = (array)Configure::read('Paginator');
$settings += $this->paginate + $defaults;

/** @var class-string<\Cake\Datasource\Paging\PaginatorInterface> $paginator */
$paginator = App::className(
$settings['className'] ?? NumericPaginator::class,
'Datasource/Paging',
'Paginator',
);
$paginator = new $paginator();
unset($settings['className']);

// Shimming 4.x to 5.x
$blacklist = [
'conditions' => 'where',
'contain' => 'contain',
];
foreacH ($blacklist as $key => $asKey) {
if (!empty($settings[$key])) {
$object->$asKey($settings[$key]);
}
}

try {
$results = $paginator->paginate(
$object,
$this->request->getQueryParams(),
$settings,
);
} catch (PageOutOfBoundsException $exception) {
throw new NotFoundException(null, null, $exception);
}

return $results;
}
```

0 comments on commit c68f53f

Please sign in to comment.