Class yii\data\ActiveDataProvider
ActiveDataProvider 基于 yii\db\Query 和 yii\db\ActiveQuery 实现了数据提供器。
ActiveDataProvider 通过使用 $query 执行数据库查询来提供数据。
以下是使用 ActiveDataProvider 提供 ActiveRecord 实例的示例:
$provider = new ActiveDataProvider([
'query' => Post::find(),
'pagination' => [
'pageSize' => 20,
],
]);
// 获取当前页的 posts
$posts = $provider->getModels();
下面的示例演示如何使用不带 ActiveRecord 的 ActiveDataProvider:
$query = new Query();
$provider = new ActiveDataProvider([
'query' => $query->from('post'),
'pagination' => [
'pageSize' => 20,
],
]);
// 获取当前页的 posts
$posts = $provider->getModels();
有关 ActiveDataProvider 的详细信息和使用信息,请参阅 guide article on data providers。
公共属性
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\base\Component |
| __clone() | yii\data\ActiveDataProvider | |
| __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() | 初始化数据库连接组件 此方法将初始化 $db 属性,以确保它引用有效的 db 连接。 | 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() | 准备数据模型和键。 | 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\ActiveDataProvider |
| setTotalCount() | 设置数据模型总数。 | yii\data\BaseDataProvider |
| trigger() | Triggers an event. | yii\base\Component |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| prepareKeys() | 准备与当前可用数据模型关联的键。 | yii\data\ActiveDataProvider |
| prepareModels() | 准备将在当前页中可用的数据模型。 | yii\data\ActiveDataProvider |
| prepareTotalCount() | 返回一个值,该值指示此数据提供器中的数据模型总数。 | yii\data\ActiveDataProvider |
属性详情
数据库连接对象或数据库连接的应用程序组件 ID。 如果未设置,将使用默认的 DB 连接。 从 2.0.2 版开始,它也可以是用于创建对象的配置数组。
用作数据模型键的列。 可以是列名,也可以是返回给定数据模型的键值的回调函数。
如果未设置,将使用以下规则确定数据模型的键:
- 如果 $query 是一个 yii\db\ActiveQuery 实例,则将使用 yii\db\ActiveQuery::$modelClass 的主键。
- 否则,将使用 $models 数组的键。
参见 getKeys().
用于获取数据模型和 $totalCount 如果未显示设置。
方法详情
| public void __clone() |
初始化数据库连接组件 此方法将初始化 $db 属性,以确保它引用有效的 db 连接。
| public void init() | ||
| throws | yii\base\InvalidConfigException | 如果 $db 不可用。 |
|---|---|---|
准备与当前可用数据模型关联的键。
| protected array prepareKeys($models) | ||
| $models | array | 可用的数据模型 |
| return | array | 键列表 |
|---|---|---|
准备将在当前页中可用的数据模型。
| protected array prepareModels() | ||
| return | array | 可用的数据模型 |
|---|---|---|
返回一个值,该值指示此数据提供器中的数据模型总数。
| protected integer prepareTotalCount() | ||
| return | integer | 此数据提供器中的数据模型总数。 |
|---|---|---|
设置此数据提供器的排序定义。
| public void setSort($value) | ||
| $value | array|yii\data\Sort|boolean | 被此数据提供器使用的排序定义。 可以是下列之一:
|
| throws | yii\base\InvalidArgumentException | |
|---|---|---|