SimplePaginator
extends NumericPaginator
in package
Simplified paginator which avoids potentially expensives queries to get the total count of records.
When using a simple paginator you will not be able to generate page numbers. Instead use only the prev/next pagination controls, and handle 404 errors when pagination goes past the available result set.
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default pagination settings.
- $_pagingParams : array<string, array<string|int, mixed>>
- Paging params after pagination operation is done.
Methods
- checkLimit() : array<string, mixed>
- Check the limit parameter and ensure it's within the maxLimit bounds.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- getDefaults() : array<string, mixed>
- Get the settings for a $model. If there are no settings for a specific repository, the general settings will be used.
- getPagingParams() : array<string, array<string|int, mixed>>
- Get paging params after pagination operation.
- mergeOptions() : array<string, mixed>
- Merges the various options that Paginator uses.
- paginate() : ResultSetInterface
- Handles automatic pagination of model records.
- setConfig() : $this
- Sets the config.
- validateSort() : array<string, mixed>
- Validate that the desired sorting can be performed on the $object.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _extractFinder() : array<string|int, mixed>
- Extracts the finder name and options out of the provided pagination options.
- _prefix() : array<string|int, mixed>
- Prefixes the field with the table alias if possible.
- _removeAliases() : array<string, mixed>
- Remove alias if needed.
- addPageCountParams() : array<string, mixed>
- Add "page" and "pageCount" params.
- addPrevNextParams() : array<string, mixed>
- Add "prevPage" and "nextPage" params.
- addSortingParams() : array<string, mixed>
- Add sorting / ordering params.
- addStartEndParams() : array<string, mixed>
- Add "start" and "end" params.
- buildParams() : array<string, mixed>
- Build pagination params.
- extractData() : array<string|int, mixed>
- Extract pagination data needed
- getAllowedParameters() : array<string|int, string>
- Shim method for reading the deprecated whitelist or allowedParameters options
- getCount() : int|null
- Simple pagination does not perform any count query, so this method returns `null`.
- getQuery() : QueryInterface
- Get query for fetching paginated results.
- getSortableFields() : array<string|int, string>|null
- Shim method for reading the deprecated sortWhitelist or sortableFields options.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Default pagination settings.
protected
array<string, mixed>
$_defaultConfig
= ['page' => 1, 'limit' => 20, 'maxLimit' => 100, 'allowedParameters' => ['limit', 'sort', 'page', 'direction']]
When calling paginate() these settings will be merged with the configuration you provide.
-
maxLimit
- The maximum limit users can choose to view. Defaults to 100 -
limit
- The initial number of items per page. Defaults to 20. -
page
- The starting page, defaults to 1. -
allowedParameters
- A list of parameters users are allowed to set using request parameters. Modifying this list will allow users to have more influence over pagination, be careful with what you permit. -
sortableFields
- A list of fields which can be used for sorting. By default all table columns can be used for sorting. You can use this option to restrict sorting only by particular fields. If you want to allow sorting on either associated columns or calculated fields then you will have to explicity specify them (along with other fields). Using an empty array will disable sorting alltogether. -
finder
- The table finder to use. Defaults toall
.
$_pagingParams
Paging params after pagination operation is done.
protected
array<string, array<string|int, mixed>>
$_pagingParams
= []
Methods
checkLimit()
Check the limit parameter and ensure it's within the maxLimit bounds.
public
checkLimit(array<string, mixed> $options) : array<string, mixed>
Parameters
- $options : array<string, mixed>
-
An array of options with a limit key to be checked.
Return values
array<string, mixed> —An array of options for pagination.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisgetConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getDefaults()
Get the settings for a $model. If there are no settings for a specific repository, the general settings will be used.
public
getDefaults(string $alias, array<string, mixed> $settings) : array<string, mixed>
Parameters
- $alias : string
-
Model name to get settings for.
- $settings : array<string, mixed>
-
The settings which is used for combining.
Return values
array<string, mixed> —An array of pagination settings for a model, or the general settings.
getPagingParams()
Get paging params after pagination operation.
public
getPagingParams() : array<string, array<string|int, mixed>>
Return values
array<string, array<string|int, mixed>>mergeOptions()
Merges the various options that Paginator uses.
public
mergeOptions(array<string, mixed> $params, array<string|int, mixed> $settings) : array<string, mixed>
Pulls settings together from the following places:
- General pagination settings
- Model specific settings.
- Request parameters
The result of this method is the aggregate of all the option sets
combined together. You can change config value allowedParameters
to modify
which options/values can be set using request parameters.
Parameters
- $params : array<string, mixed>
-
Request params.
- $settings : array<string|int, mixed>
-
The settings to merge with the request data.
Return values
array<string, mixed> —Array of merged options.
paginate()
Handles automatic pagination of model records.
public
paginate(RepositoryInterface|QueryInterface $object[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $settings = [] ]) : ResultSetInterface
Configuring pagination
When calling paginate()
you can use the $settings parameter to pass in
pagination settings. These settings are used to build the queries made
and control other pagination settings.
If your settings contain a key with the current table's alias. The data inside that key will be used. Otherwise, the top level configuration will be used.
$settings = [
'limit' => 20,
'maxLimit' => 100
];
$results = $paginator->paginate($table, $settings);
The above settings will be used to paginate any repository. You can configure repository specific settings by keying the settings with the repository alias.
$settings = [
'Articles' => [
'limit' => 20,
'maxLimit' => 100
],
'Comments' => [ ... ]
];
$results = $paginator->paginate($table, $settings);
This would allow you to have different pagination settings for
Articles
and Comments
repositories.
Controlling sort fields
By default CakePHP will automatically allow sorting on any column on the
repository object being paginated. Often times you will want to allow
sorting on either associated columns or calculated fields. In these cases
you will need to define an allowed list of all the columns you wish to allow
sorting on. You can define the allowed sort fields in the $settings
parameter:
$settings = [
'Articles' => [
'finder' => 'custom',
'sortableFields' => ['title', 'author_id', 'comment_count'],
]
];
Passing an empty array as sortableFields disallows sorting altogether.
Paginating with custom finders
You can paginate with any find type defined on your table using the
finder
option.
$settings = [
'Articles' => [
'finder' => 'popular'
]
];
$results = $paginator->paginate($table, $settings);
Would paginate using the find('popular')
method.
You can also pass an already created instance of a query to this method:
$query = $this->Articles->find('popular')->matching('Tags', function ($q) {
return $q->where(['name' => 'CakePHP'])
});
$results = $paginator->paginate($query);
Scoping Request parameters
By using request parameter scopes you can paginate multiple queries in the same controller action:
$articles = $paginator->paginate($articlesQuery, ['scope' => 'articles']);
$tags = $paginator->paginate($tagsQuery, ['scope' => 'tags']);
Each of the above queries will use different query string parameter sets for pagination data. An example URL paginating both results would be:
/dashboard?articles[page]=1&tags[page]=2
Parameters
- $object : RepositoryInterface|QueryInterface
-
The repository or query to paginate.
- $params : array<string|int, mixed> = []
-
Request params
- $settings : array<string|int, mixed> = []
-
The settings/configuration used for pagination.
Tags
Return values
ResultSetInterface —Query results
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thisvalidateSort()
Validate that the desired sorting can be performed on the $object.
public
validateSort(RepositoryInterface $object, array<string, mixed> $options) : array<string, mixed>
Only fields or virtualFields can be sorted on. The direction param will also be sanitized. Lastly sort + direction keys will be converted into the model friendly order key.
You can use the allowedParameters option to control which columns/fields are available for sorting via URL parameters. This helps prevent users from ordering large result sets on un-indexed values.
If you need to sort on associated columns or synthetic properties you
will need to use the sortableFields
option.
Any columns listed in the allowed sort fields will be implicitly trusted. You can use this to sort on synthetic columns, or columns added in custom find operations that may not exist in the schema.
The default order options provided to paginate() will be merged with the user's requested sorting field/direction.
Parameters
- $object : RepositoryInterface
-
Repository object.
- $options : array<string, mixed>
-
The pagination options being used for this request.
Return values
array<string, mixed> —An array of options with sort + direction removed and replaced with order if possible.
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_extractFinder()
Extracts the finder name and options out of the provided pagination options.
protected
_extractFinder(array<string, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string, mixed>
-
the pagination options.
Return values
array<string|int, mixed> —An array containing in the first position the finder name and in the second the options to be passed to it.
_prefix()
Prefixes the field with the table alias if possible.
protected
_prefix(RepositoryInterface $object, array<string|int, mixed> $order[, bool $allowed = false ]) : array<string|int, mixed>
Parameters
- $object : RepositoryInterface
-
Repository object.
- $order : array<string|int, mixed>
-
Order array.
- $allowed : bool = false
-
Whether the field was allowed.
Return values
array<string|int, mixed> —Final order array.
_removeAliases()
Remove alias if needed.
protected
_removeAliases(array<string, mixed> $fields, string $model) : array<string, mixed>
Parameters
- $fields : array<string, mixed>
-
Current fields
- $model : string
-
Current model alias
Return values
array<string, mixed> —$fields Unaliased fields where applicable
addPageCountParams()
Add "page" and "pageCount" params.
protected
addPageCountParams(array<string, mixed> $params, array<string|int, mixed> $data) : array<string, mixed>
Parameters
- $params : array<string, mixed>
-
Paging params.
- $data : array<string|int, mixed>
-
Paginator data.
Return values
array<string, mixed> —Updated params.
addPrevNextParams()
Add "prevPage" and "nextPage" params.
protected
addPrevNextParams(array<string, mixed> $params, array<string|int, mixed> $data) : array<string, mixed>
Parameters
- $params : array<string, mixed>
-
Paginator params.
- $data : array<string|int, mixed>
-
Paging data.
Return values
array<string, mixed> —Updated params.
addSortingParams()
Add sorting / ordering params.
protected
addSortingParams(array<string, mixed> $params, array<string|int, mixed> $data) : array<string, mixed>
Parameters
- $params : array<string, mixed>
-
Paginator params.
- $data : array<string|int, mixed>
-
Paging data.
Return values
array<string, mixed> —Updated params.
addStartEndParams()
Add "start" and "end" params.
protected
addStartEndParams(array<string, mixed> $params, array<string|int, mixed> $data) : array<string, mixed>
Parameters
- $params : array<string, mixed>
-
Paging params.
- $data : array<string|int, mixed>
-
Paginator data.
Return values
array<string, mixed> —Updated params.
buildParams()
Build pagination params.
protected
buildParams(array<string, mixed> $data) : array<string, mixed>
Parameters
- $data : array<string, mixed>
-
Paginator data containing keys 'options', 'count', 'defaults', 'finder', 'numResults'.
Return values
array<string, mixed> —Paging params.
extractData()
Extract pagination data needed
protected
extractData(RepositoryInterface $object, array<string, mixed> $params, array<string, mixed> $settings) : array<string|int, mixed>
Parameters
- $object : RepositoryInterface
-
The repository object.
- $params : array<string, mixed>
-
Request params
- $settings : array<string, mixed>
-
The settings/configuration used for pagination.
Return values
array<string|int, mixed> —Array with keys 'defaults', 'options' and 'finder'
getAllowedParameters()
Shim method for reading the deprecated whitelist or allowedParameters options
protected
getAllowedParameters() : array<string|int, string>
Return values
array<string|int, string>getCount()
Simple pagination does not perform any count query, so this method returns `null`.
protected
getCount(QueryInterface $query, array<string|int, mixed> $data) : int|null
Parameters
- $query : QueryInterface
-
Query instance.
- $data : array<string|int, mixed>
-
Pagination data.
Return values
int|nullgetQuery()
Get query for fetching paginated results.
protected
getQuery(RepositoryInterface $object, QueryInterface|null $query, array<string, mixed> $data) : QueryInterface
Parameters
- $object : RepositoryInterface
-
Repository instance.
- $query : QueryInterface|null
-
Query Instance.
- $data : array<string, mixed>
-
Pagination data.
Return values
QueryInterfacegetSortableFields()
Shim method for reading the deprecated sortWhitelist or sortableFields options.
protected
getSortableFields(array<string, mixed> $config) : array<string|int, string>|null
Parameters
- $config : array<string, mixed>
-
The configuration data to coalesce and emit warnings on.