BaseDataProvider
API reference › yii › data › BaseDataProvider
3 subclasses
BaseDataProvider provides a base class that implements the DataProviderInterface.
For more details and usage information on BaseDataProvider, see the guide article on data providers.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$count read-only | int | The number of data models in the current page. | BaseDataProvider |
$id | string| | An ID that uniquely identifies the data provider among all data providers. Default: null. | BaseDataProvider |
$keys | array | The list of key values corresponding to $models. | BaseDataProvider |
$models | array | The list of data models in the current page. | BaseDataProvider |
$pagination | array|Pagination| | The pagination to be used by this data provider. | BaseDataProvider |
$sort | array|Sort| | The sort definition to be used by this data provider. | BaseDataProvider |
$totalCount | int | Total number of possible data models. | BaseDataProvider |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getCount() | Returns the number of data models in the current page. | BaseDataProvider |
getKeys() | Returns the key values associated with the data models. | BaseDataProvider |
getModels() | Returns the data models in the current page. | BaseDataProvider |
getPagination() | Returns the pagination object used by this data provider. | BaseDataProvider |
getSort() | Returns the sorting object used by this data provider. | BaseDataProvider |
getTotalCount() | Returns the total number of data models. | BaseDataProvider |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | BaseDataProvider |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
prepare() | Prepares the data models and keys. | BaseDataProvider |
refresh() | Refreshes the data provider. | BaseDataProvider |
setKeys() | Sets the key values associated with the data models. | BaseDataProvider |
setModels() | Sets the data models in the current page. | BaseDataProvider |
setPagination() | Sets the pagination for this data provider. | BaseDataProvider |
setSort() | Sets the sort definition for this data provider. | BaseDataProvider |
setTotalCount() | Sets the total number of data models. | BaseDataProvider |
trigger() | Triggers an event. | yii\ |
prepareKeys() protected abstract | Prepares the keys associated with the currently available data models. | BaseDataProvider |
prepareModels() protected abstract | Prepares the data models that will be made available in the current page. | BaseDataProvider |
prepareTotalCount() protected abstract | Returns a value indicating the total number of data models in this data provider. | BaseDataProvider |
Property details
Section titled “Property details”public string|null $id = nullAn ID that uniquely identifies the data provider among all data providers.
Generated automatically the following way in case it is not set:
- First data provider ID is empty.
- Second and all subsequent data provider IDs are: “dp-1”, “dp-2”, etc.
$keys
Section titled “$keys ”public array $keysThe list of key values corresponding to $models.
Each data model in $models is
uniquely identified by the corresponding key value in this array.
$pagination
Section titled “$pagination ”public array|yii\data\Pagination|false $paginationThe pagination to be used by this data provider.
This can be one of the following:
- a configuration array for creating the pagination object. The “class” element defaults to ‘yii\data\Pagination’
- an instance of
Paginationor its subclass - false, if pagination needs to be disabled.
$sort
Section titled “$sort ”public array|yii\data\Sort|bool $sortThe sort definition to be used by this data provider.
This can be one of the following:
- a configuration array for creating the sort definition object. The “class” element defaults to ‘yii\data\Sort’
- an instance of
Sortor its subclass - false, if sorting needs to be disabled.
Method details
Section titled “Method details”getCount()
Section titled “getCount() ”public function getCount(): intReturns the number of data models in the current page.
Returns
int: The number of data models in the current page.
getKeys()
Section titled “getKeys() ”public function getKeys(): arrayReturns the key values associated with the data models.
getModels()
Section titled “getModels() ”public function getModels(): arrayReturns the data models in the current page.
Returns
array: The list of data models in the current page.
getPagination()
Section titled “getPagination() ”public function getPagination(): yii\data\Pagination|falseReturns the pagination object used by this data provider.
Note that you should call prepare() or getModels() first to get correct values
of Pagination::$totalCount and Pagination::$pageCount.
Returns
Pagination|false: The pagination object. If this is false, it means the pagination is disabled.
getSort()
Section titled “getSort() ”public function getSort(): yii\data\Sort|boolReturns the sorting object used by this data provider.
Returns
Sort|bool: The sorting object. If this is false, it means the sorting is disabled.
getTotalCount()
Section titled “getTotalCount() ”public function getTotalCount(): intReturns the total number of data models.
When $pagination is false, this returns the same value as $count.
Otherwise, it will call prepareTotalCount() to get the count.
Returns
int: Total number of possible data models.
init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
prepare()
Section titled “prepare() ”public function prepare(bool $forcePrepare = false): mixedPrepares the data models and keys.
This method will prepare the data models and keys that can be retrieved via
getModels() and getKeys().
This method will be implicitly called by getModels() and getKeys() if it has not been called before.
Parameters
| Parameter | Type | Description |
|---|---|---|
$forcePrepare | bool | Whether to force data preparation even if it has been done before. |
Returns
mixed
refresh()
Section titled “refresh() ”public function refresh(): mixedRefreshes the data provider.
After calling this method, if getModels(), getKeys() or getTotalCount() is called again,
they will re-execute the query and return the latest data available.
Returns
mixed
setKeys()
Section titled “setKeys() ”public function setKeys(array $keys): mixedSets the key values associated with the data models.
Parameters
| Parameter | Type | Description |
|---|---|---|
$keys | array | The list of key values corresponding to $models. |
Returns
mixed
setModels()
Section titled “setModels() ”public function setModels(array $models): mixedSets the data models in the current page.
Parameters
| Parameter | Type | Description |
|---|---|---|
$models | array | The models in the current page |
Returns
mixed
setPagination()
Section titled “setPagination() ”public function setPagination(array|yii\data\Pagination|false $value): mixedSets the pagination for this data provider.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | array|Pagination| | The pagination to be used by this data provider. This can be one of the following: - a configuration array for creating the pagination object. The “class” element defaults to ‘yii\data\Pagination’ - an instance of Pagination or its subclass - false, if pagination needs to be disabled. |
Returns
mixed
Throws
setSort()
Section titled “setSort() ”public function setSort(array|yii\data\Sort|bool $value): mixedSets the sort definition for this data provider.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | array|Sort| | The sort definition to be used by this data provider. This can be one of the following: - a configuration array for creating the sort definition object. The “class” element defaults to ‘yii\data\Sort’ - an instance of Sort or its subclass - false, if sorting needs to be disabled. |
Returns
mixed
Throws
setTotalCount()
Section titled “setTotalCount() ”public function setTotalCount(int $value): mixedSets the total number of data models.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | int | The total number of data models. |
Returns
mixed
prepareKeys()
Section titled “prepareKeys() ”abstract protected function prepareKeys(array $models): arrayPrepares the keys associated with the currently available data models.
Parameters
| Parameter | Type | Description |
|---|---|---|
$models | array | The available data models |
Returns
array: The keys
prepareModels()
Section titled “prepareModels() ”abstract protected function prepareModels(): arrayPrepares the data models that will be made available in the current page.
Returns
array: The available data models
prepareTotalCount()
Section titled “prepareTotalCount() ”abstract protected function prepareTotalCount(): intReturns a value indicating the total number of data models in this data provider.
Returns
int: Total number of data models in this data provider.