Class yii\data\ArrayDataProvider
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.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
$allModels | array | The data that is not paginated or sorted. | yii\data\ArrayDataProvider |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$count | integer | 当前页中的数据模型数。 | yii\data\BaseDataProvider |
$id | string | 在所有数据提供器中唯一标识该数据提供器的 ID,如果未设置该 ID, 则按以下方式自动生成: - 第一个数据提供器 ID 为空。 - 第二个和所有后续的数据提供者 IDs 是:"dp-1","dp-2" 等。 | yii\data\BaseDataProvider |
$key | string|callable | The column that is used as the key of the data models. | yii\data\ArrayDataProvider |
$keys | array | 与 models 对应的键值列表。models 中的每个数据模型 都由该数组中相应的键值唯一标识。 | yii\data\BaseDataProvider |
$modelClass | string | The name of the Model class that will be represented. | yii\data\ArrayDataProvider |
$models | array | 当前页中的数据模型列表。 | yii\data\BaseDataProvider |
$pagination | yii\data\Pagination|false | 分页对象。如果为 false,则表示禁用分页。 | yii\data\BaseDataProvider |
$sort | yii\data\Sort|boolean | 排序对象。如果为 false,则表示排序被禁用。 | yii\data\BaseDataProvider |
$totalCount | integer | 数据模型总数。 | yii\data\BaseDataProvider |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__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() | 返回当前页中的数据模型数。 | yii\data\BaseDataProvider |
getKeys() | 返回与数据模型关联的键值。 | yii\data\BaseDataProvider |
getModels() | 返回当前页中的数据模型。 | yii\data\BaseDataProvider |
getPagination() | 返回此数据提供器使用的分页对象。 注意,我们应该先调用 prepare() 或者 getModels() 以获取 yii\data\Pagination::$totalCount 和 yii\data\Pagination::$pageCount 的正确的值。 | yii\data\BaseDataProvider |
getSort() | 返回此数据提供器使用的排序对象。 | yii\data\BaseDataProvider |
getTotalCount() | 返回数据模型总数。 当 $pagination 为 false,值与 $count 相同。 否则,将调用 prepareTotalCount() 获取数量。 | 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() | 准备数据模型和键。 | yii\data\BaseDataProvider |
refresh() | 刷新数据提供器 调用此方法后,如果再次调用 getModels(),getKeys() 或者 getTotalCount(), 它们将重新执行查询并返回可用的最新数据。 | yii\data\BaseDataProvider |
setKeys() | 设置与数据模型关联的键值。 | yii\data\BaseDataProvider |
setModels() | 设置当前页中的数据模型。 | yii\data\BaseDataProvider |
setPagination() | 为数据提供器设置分页组件。 | yii\data\BaseDataProvider |
setSort() | 设置此数据提供器的排序定义。 | yii\data\BaseDataProvider |
setTotalCount() | 设置数据模型总数。 | yii\data\BaseDataProvider |
trigger() | Triggers an event. | yii\base\Component |
受保护的方法
方法 | 描述 | 被定义在 |
---|---|---|
prepareKeys() | 准备与当前可用数据模型关联的键。 | yii\data\ArrayDataProvider |
prepareModels() | 准备将在当前页中可用的数据模型。 | yii\data\ArrayDataProvider |
prepareTotalCount() | 返回一个值,该值指示此数据提供器中的数据模型总数。 | yii\data\ArrayDataProvider |
sortModels() | Sorts the data models according to the given sort definition. | yii\data\ArrayDataProvider |
属性详情
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.
参见 getKeys().
The name of the Model class that will be represented. This property is used to get columns' names.
方法详情
准备与当前可用数据模型关联的键。
protected array prepareKeys($models) | ||
$models | array | 可用的数据模型 |
return | array | 键列表 |
---|
准备将在当前页中可用的数据模型。
protected array prepareModels() | ||
return | array | 可用的数据模型 |
---|
返回一个值,该值指示此数据提供器中的数据模型总数。
protected integer prepareTotalCount() | ||
return | integer | 此数据提供器中的数据模型总数。 |
---|
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 |
---|