W3cubDocs

/Yii 2.0

Class yii\data\ActiveDataProvider

Inheritance yii\data\ActiveDataProvider » yii\data\BaseDataProvider » yii\base\Component » yii\base\Object
Implements yii\base\Configurable, yii\data\DataProviderInterface
Subclasses yii\elasticsearch\ActiveDataProvider, yii\sphinx\ActiveDataProvider
Available since version 2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/data/ActiveDataProvider.php

ActiveDataProvider implements a data provider based on yii\db\Query and yii\db\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.

Public Properties

Property Type Description Defined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$count integer The number of data models in the current page. yii\data\BaseDataProvider
$db yii\db\Connection|array|string The DB connection object or the application component ID of the DB connection. yii\data\ActiveDataProvider
$id string An ID that uniquely identifies the data provider among all data providers. yii\data\BaseDataProvider
$key string|callable The column that is used as the key of the data models. yii\data\ActiveDataProvider
$keys array The list of key values corresponding to $models. yii\data\BaseDataProvider
$models array The list of data models in the current page. yii\data\BaseDataProvider
$pagination yii\data\Pagination|false The pagination object. yii\data\BaseDataProvider
$query yii\db\QueryInterface The query that is used to fetch data models and $totalCount if it is not explicitly set. yii\data\ActiveDataProvider
$sort yii\data\Sort|boolean The sorting object. yii\data\BaseDataProvider
$totalCount integer Total number of possible data models. yii\data\BaseDataProvider

Public Methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\Object
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
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\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
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. yii\data\BaseDataProvider
getKeys() Returns the key values associated with the data models. yii\data\BaseDataProvider
getModels() Returns the data models in the current page. yii\data\BaseDataProvider
getPagination() Returns the pagination object used by this data provider. yii\data\BaseDataProvider
getSort() Returns the sorting object used by this data provider. yii\data\BaseDataProvider
getTotalCount() Returns the total number of data models. yii\data\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\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the DB connection component. yii\data\ActiveDataProvider
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. yii\data\BaseDataProvider
refresh() Refreshes the data provider. yii\data\BaseDataProvider
setKeys() Sets the key values associated with the data models. yii\data\BaseDataProvider
setModels() Sets the data models in the current page. yii\data\BaseDataProvider
setPagination() Sets the pagination for this data provider. yii\data\BaseDataProvider
setSort() Sets the sort definition for this data provider. yii\data\ActiveDataProvider
setTotalCount() Sets the total number of data models. yii\data\BaseDataProvider
trigger() Triggers an event. yii\base\Component

Protected Methods

Method Description Defined By
prepareKeys() Prepares the keys associated with the currently available data models. yii\data\ActiveDataProvider
prepareModels() Prepares the data models that will be made available in the current page. yii\data\ActiveDataProvider
prepareTotalCount() Returns a value indicating the total number of data models in this data provider. yii\data\ActiveDataProvider

Property Details

$db public property

The DB connection object or the application component ID of the DB connection. If not set, the default DB connection will be used. Starting from version 2.0.2, this can also be a configuration array for creating the object.

public yii\db\Connection|array|string $db = null

$key public property

The 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:

See also getKeys().

public string|callable $key = null

$query public property

The query that is used to fetch data models and $totalCount if it is not explicitly set.

public yii\db\QueryInterface $query = null

Method Details

init() public method

Initializes the DB connection component.

This method will initialize the $db property to make sure it refers to a valid DB connection.

public void init ( )
throws yii\base\InvalidConfigException

if $db is invalid.

prepareKeys() protected method

Prepares the keys associated with the currently available data models.

protected array prepareKeys ( $models )
$models array

The available data models

return array

The keys

prepareModels() protected method

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

protected array prepareModels ( )
return array

The available data models

prepareTotalCount() protected method

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

protected integer prepareTotalCount ( )
return integer

Total number of data models in this data provider.

setSort() public method

Sets the sort definition for this data provider.

public void setSort ( $value )
$value array|yii\data\Sort|boolean

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 yii\data\Sort or its subclass
  • false, if sorting needs to be disabled.
throws yii\base\InvalidParamException

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-data-activedataprovider.html