Class yii\data\ArrayDataProvider
Inheritance | yii\data\ArrayDataProvider » yii\data\BaseDataProvider » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable, yii\data\DataProviderInterface |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/data/ArrayDataProvider.php |
ArrayDataProvider implements a data provider based on a data array.
The $allModels property contains all data models that may be sorted and/or paginated. ArrayDataProvider will provide the data after sorting and/or pagination. You may configure the $sort and $pagination properties to customize the sorting and pagination behaviors.
Elements in the $allModels array may be either objects (e.g. model objects) or associative arrays (e.g. query results of DAO). Make sure to set the $key property to the name of the field that uniquely identifies a data record or false if you do not have such a field.
Compared to yii\data\ActiveDataProvider, ArrayDataProvider could be less efficient because it needs to have $allModels ready.
ArrayDataProvider may be used in the following way:
$query = new Query;
$provider = new ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
// get the posts in the current page
$posts = $provider->getModels();
Note: if you want to use the sorting feature, you must configure the $sort property so that the provider knows which columns can be sorted.
For more details and usage information on ArrayDataProvider, see the guide article on data providers.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$allModels | array | The data that is not paginated or sorted. | yii\data\ArrayDataProvider |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component |
$count | integer | The number of data models in the current page. This property is read-only. | yii\data\BaseDataProvider |
$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\ArrayDataProvider |
$keys | 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. | yii\data\BaseDataProvider |
$modelClass | string | The name of the Model class that will be represented. | yii\data\ArrayDataProvider |
$models | array | The list of data models in the current page. | yii\data\BaseDataProvider |
$pagination | yii\data\Pagination|false | The pagination object. If this is false, it means the pagination is disabled. Note that the type of this property differs in getter and setter. See getPagination() and setPagination() for details. | yii\data\BaseDataProvider |
$sort | yii\data\Sort|boolean | The sorting object. If this is false, it means the sorting is disabled. Note that the type of this property differs in getter and setter. See getSort() and setSort() for details. | 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\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\BaseObject |
__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() | Returns 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. | 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\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\data\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. | 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\BaseDataProvider |
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\ArrayDataProvider |
prepareModels() | Prepares the data models that will be made available in the current page. | yii\data\ArrayDataProvider |
prepareTotalCount() | Returns a value indicating the total number of data models in this data provider. | yii\data\ArrayDataProvider |
sortModels() | Sorts the data models according to the given sort definition. | yii\data\ArrayDataProvider |
Property Details
The data that is not paginated or sorted. When pagination is enabled, this property usually contains more elements than $models. The array elements must use zero-based integer keys.
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 index of the $models array will be used.
See also getKeys().
The name of the Model class that will be represented. This property is used to get columns' names.
Method Details
Prepares the keys associated with the currently available data models.
protected array prepareKeys ( $models ) | ||
$models | array | The available data models |
return | array | The keys |
---|
Prepares the data models that will be made available in the current page.
protected array prepareModels ( ) | ||
return | array | The available data models |
---|
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. |
---|
Sorts the data models according to the given sort definition.
protected array sortModels ( $models, $sort ) | ||
$models | array | The models to be sorted |
$sort | yii\data\Sort | The sort definition |
return | array | The sorted data models |
---|
Signup or Login in order to comment.