ActiveDataProvider
API reference › yii › data › ActiveDataProvider
ActiveDataProvider implements a data provider based on Query and ActiveQuery.
ActiveDataProvider provides data by performing DB queries using $query.
The following is an example of using ActiveDataProvider to provide ActiveRecord instances:
$provider = new ActiveDataProvider([ 'query' => Post::find(), 'pagination' => [ 'pageSize' => 20, ],]);
// get the posts in the current page$posts = $provider->getModels();And the following example shows how to use ActiveDataProvider without ActiveRecord:
$query = new Query();$provider = new ActiveDataProvider([ 'query' => $query->from('post'), 'pagination' => [ 'pageSize' => 20, ],]);
// get the posts in the current page$posts = $provider->getModels();For more details and usage information on ActiveDataProvider, 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 |
$db | yii\| | The DB connection object or the application component ID of the DB connection. Default: null. | ActiveDataProvider |
$id | string| | An ID that uniquely identifies the data provider among all data providers. Default: null. | BaseDataProvider |
$key | string| | The column that is used as the key of the data models. Default: null. | ActiveDataProvider |
$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 |
$query | yii\| | The query that is used to fetch data models and $totalCount if it is not explicitly set. Default: null. | ActiveDataProvider |
$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() | ActiveDataProvider | |
__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 DB connection component. | ActiveDataProvider |
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. | ActiveDataProvider |
setTotalCount() | Sets the total number of data models. | BaseDataProvider |
trigger() | Triggers an event. | yii\ |
prepareKeys() protected | Prepares the keys associated with the currently available data models. | ActiveDataProvider |
prepareModels() protected | Prepares the data models that will be made available in the current page. | ActiveDataProvider |
prepareTotalCount() protected | Returns a value indicating the total number of data models in this data provider. | ActiveDataProvider |
Property details
Section titled “Property details”public yii\db\Connection|array|string|null $db = nullThe DB connection object or the application component ID of the DB connection.
If set it overrides $query default DB connection.
Starting from version 2.0.2, this can also be a configuration array for creating the object.
public string|callable|null $key = nullThe column that is used as the key of the data models.
This can be either a column name, or a callable that returns the key value of a given data model.
If this is not set, the following rules will be used to determine the keys of the data models:
- If
$queryis anActiveQueryinstance, the primary keys ofActiveQuery::$modelClasswill be used. - Otherwise, the keys of the
$modelsarray will be used.
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes the DB connection component.
This method will initialize the $db property (when set) to make sure it refers to a valid DB connection.
setSort()
Section titled “setSort() ”public function setSort(mixed $value): mixedSets the sort definition for this data provider.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | 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
prepareKeys()
Section titled “prepareKeys() ”protected function prepareKeys(mixed $models): arrayPrepares the keys associated with the currently available data models.
Parameters
| Parameter | Type | Description |
|---|---|---|
$models | mixed | The available data models |
Returns
array: The keys
prepareModels()
Section titled “prepareModels() ”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() ”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.