Skip to content
PHP Ecosystem
Explore

BaseDataProvider

API reference › yii › data › BaseDataProvider

abstract classyii\data\BaseDataProvider
Extendsyii\base\Component » yii\base\BaseObject
ImplementsDataProviderInterface, yii\base\Configurable
Since2.0
Sourceframework/data/BaseDataProvider.php
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.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$count read-onlyintThe number of data models in the current page.BaseDataProvider
$idstring|nullAn ID that uniquely identifies the data provider among all data providers. Default: null.BaseDataProvider
$keysarrayThe list of key values corresponding to $models.BaseDataProvider
$modelsarrayThe list of data models in the current page.BaseDataProvider
$paginationarray|Pagination|falseThe pagination to be used by this data provider.BaseDataProvider
$sortarray|Sort|boolThe sort definition to be used by this data provider.BaseDataProvider
$totalCountintTotal number of possible data models.BaseDataProvider
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
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\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.BaseDataProvider
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
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\base\Component
prepareKeys() protected abstractPrepares the keys associated with the currently available data models.BaseDataProvider
prepareModels() protected abstractPrepares the data models that will be made available in the current page.BaseDataProvider
prepareTotalCount() protected abstractReturns a value indicating the total number of data models in this data provider.BaseDataProvider
public string|null $id = null

An 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.
public array $keys

The list of key values corresponding to $models.

Each data model in $models is uniquely identified by the corresponding key value in this array.

public array|yii\data\Pagination|false $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.
public array|yii\data\Sort|bool $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.
public function getCount(): int

Returns the number of data models in the current page.

Returns

int: The number of data models in the current page.

public function getKeys(): array

Returns the key values associated with the data models.

Returns

array: The list of key values corresponding to $models. Each data model in $models is uniquely identified by the corresponding key value in this array.

public function getModels(): array

Returns the data models in the current page.

Returns

array: The list of data models in the current page.

public function getPagination(): yii\data\Pagination|false

Returns 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.

public function getSort(): yii\data\Sort|bool

Returns the sorting object used by this data provider.

Returns

Sort|bool: The sorting object. If this is false, it means the sorting is disabled.

public function getTotalCount(): int

Returns 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.

public function init(): mixed

Initializes the object.

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

Returns

mixed

public function prepare(bool $forcePrepare = false): mixed

Prepares 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

ParameterTypeDescription
$forcePrepareboolWhether to force data preparation even if it has been done before.

Returns

mixed

public function refresh(): mixed

Refreshes 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

public function setKeys(array $keys): mixed

Sets the key values associated with the data models.

Parameters

ParameterTypeDescription
$keysarrayThe list of key values corresponding to $models.

Returns

mixed

public function setModels(array $models): mixed

Sets the data models in the current page.

Parameters

ParameterTypeDescription
$modelsarrayThe models in the current page

Returns

mixed

public function setPagination(array|yii\data\Pagination|false $value): mixed

Sets the pagination for this data provider.

Parameters

ParameterTypeDescription
$valuearray|Pagination|falseThe 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

public function setSort(array|yii\data\Sort|bool $value): mixed

Sets the sort definition for this data provider.

Parameters

ParameterTypeDescription
$valuearray|Sort|boolThe 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

public function setTotalCount(int $value): mixed

Sets the total number of data models.

Parameters

ParameterTypeDescription
$valueintThe total number of data models.

Returns

mixed

abstract protected function prepareKeys(array $models): array

Prepares the keys associated with the currently available data models.

Parameters

ParameterTypeDescription
$modelsarrayThe available data models

Returns

array: The keys

abstract protected function prepareModels(): array

Prepares the data models that will be made available in the current page.

Returns

array: The available data models

abstract protected function prepareTotalCount(): int

Returns a value indicating the total number of data models in this data provider.

Returns

int: Total number of data models in this data provider.