Paginator

Type
Class
Namespace
craft\db
Inherits
craft\db\Paginator » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.1.19

Query Paginator

use craft\db\Paginator;

$paginator = new Paginator($query, [
    'pageSize' => 10,
    'currentPage' => \Craft::$app->request->pageNum,
]);

$pageResults = $paginator->getPageResults();
{% set paginator = create('craft\\db\\Paginator', [query, {
  pageSize: 10,
  currentPage: craft.app.request.pageNum,
}]) %}

{% set pageResults = paginator.getPageResults() %}

View source (opens new window)

# Public Properties

Property Description
db yii\db\Connection (opens new window), null (opens new window) – The DB connection to be used with the query.
pageOffset integer (opens new window), float (opens new window)
pageResults array (opens new window)
pageSize integer (opens new window) – The number of results to include for each page

# db

Type
yii\db\Connection (opens new window), null (opens new window)
Default value
null

The DB connection to be used with the query. If null, the query will choose the connection to use.

View source (opens new window)

# pageOffset

Type
integer (opens new window), float (opens new window)
Default value
null
Access
Read-only

View source (opens new window)

# pageResults

Type
array (opens new window)
Default value
null

View source (opens new window)

# pageSize

Type
integer (opens new window)
Default value
100

The number of results to include for each page

View source (opens new window)

# Protected Properties

Property Description
currentPage integer (opens new window) – The current page
query yii\db\QueryInterface (opens new window), yii\db\Query (opens new window) – The query being paginated
totalPages integer (opens new window) – The total number of pages
totalResults integer (opens new window) – The total query count

# currentPage

Type
integer (opens new window)
Default value
1

The current page

View source (opens new window)

# query

Type
yii\db\QueryInterface (opens new window), yii\db\Query (opens new window)
Default value
null

The query being paginated

View source (opens new window)

# totalPages

Type
integer (opens new window)
Default value
null

The total number of pages

View source (opens new window)

# totalResults

Type
integer (opens new window)
Default value
null

The total query count

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__construct() Constructor
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
getCurrentPage() Returns the current page
getPageOffset() Returns the offset of the first result returned by getPageResults()
getPageResults() Returns the results for the current page
getTotalPages() Returns the total number of pages
getTotalResults() Returns the total number of query results
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() Initializes the object.
setCurrentPage() Sets the current page
setPageResults() Sets the results for the current page.

# __construct()

Constructor

View source (opens new window)

Arguments

# getCurrentPage()

Returns the current page

View source (opens new window)

Returns

integer (opens new window)

# getPageOffset()

Returns the offset of the first result returned by getPageResults()

View source (opens new window)

Returns

integer (opens new window), float (opens new window)

# getPageResults()

Returns the results for the current page

View source (opens new window)

Returns

array (opens new window)

# getTotalPages()

Returns the total number of pages

View source (opens new window)

Returns

integer (opens new window)

# getTotalResults()

Returns the total number of query results

View source (opens new window)

Returns

integer (opens new window), float (opens new window)

# init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source (opens new window)

Throws

# setCurrentPage()

Sets the current page

View source (opens new window)

Arguments

# setPageResults()

Since
3.1.22

Sets the results for the current page.

View source (opens new window)

Arguments

  • $pageResults