3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
10
use yii\base\InvalidConfigException;
12
use yii\db\ActiveQueryInterface;
14
use yii\db\QueryInterface;
18
* ActiveDataProvider implements a data provider based on [[\yii\db\Query]] and [[\yii\db\ActiveQuery]].
20
* ActiveDataProvider provides data by performing DB queries using [[query]].
22
* The following is an example of using ActiveDataProvider to provide ActiveRecord instances:
25
* $provider = new ActiveDataProvider([
26
* 'query' => Post::find(),
32
* // get the posts in the current page
33
* $posts = $provider->getModels();
36
* And the following example shows how to use ActiveDataProvider without ActiveRecord:
39
* $query = new Query();
40
* $provider = new ActiveDataProvider([
41
* 'query' => $query->from('post'),
47
* // get the posts in the current page
48
* $posts = $provider->getModels();
51
* For more details and usage information on ActiveDataProvider, see the [guide article on data providers](guide:output-data-providers).
53
* @author Qiang Xue <qiang.xue@gmail.com>
56
class ActiveDataProvider extends BaseDataProvider
59
* @var QueryInterface|null the query that is used to fetch data models and [[totalCount]] if it is not explicitly set.
63
* @var string|callable|null the column that is used as the key of the data models.
64
* This can be either a column name, or a callable that returns the key value of a given data model.
66
* If this is not set, the following rules will be used to determine the keys of the data models:
68
* - If [[query]] is an [[\yii\db\ActiveQuery]] instance, the primary keys of [[\yii\db\ActiveQuery::modelClass]] will be used.
69
* - Otherwise, the keys of the [[models]] array will be used.
75
* @var Connection|array|string|null the DB connection object or the application component ID of the DB connection.
76
* If set it overrides [[query]] default DB connection.
77
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
83
* Initializes the DB connection component.
84
* This method will initialize the [[db]] property (when set) to make sure it refers to a valid DB connection.
85
* @throws InvalidConfigException if [[db]] is invalid.
87
public function init()
90
if ($this->db !== null) {
91
$this->db = Instance::ensure($this->db);
98
protected function prepareModels()
100
if (!$this->query instanceof QueryInterface) {
101
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
103
$query = clone $this->query;
104
if (($pagination = $this->getPagination()) !== false) {
105
$pagination->totalCount = $this->getTotalCount();
106
if ($pagination->totalCount === 0) {
109
$query->limit($pagination->getLimit())->offset($pagination->getOffset());
111
if (($sort = $this->getSort()) !== false) {
112
$query->addOrderBy($sort->getOrders());
115
return $query->all($this->db);
121
protected function prepareKeys($models)
124
if ($this->key !== null) {
125
foreach ($models as $model) {
126
if (is_string($this->key)) {
127
$keys[] = $model[$this->key];
129
$keys[] = call_user_func($this->key, $model);
134
} elseif ($this->query instanceof ActiveQueryInterface) {
135
/* @var $class \yii\db\ActiveRecordInterface */
136
$class = $this->query->modelClass;
137
$pks = $class::primaryKey();
138
if (count($pks) === 1) {
140
foreach ($models as $model) {
141
$keys[] = $model[$pk];
144
foreach ($models as $model) {
146
foreach ($pks as $pk) {
147
$kk[$pk] = $model[$pk];
156
return array_keys($models);
162
protected function prepareTotalCount()
164
if (!$this->query instanceof QueryInterface) {
165
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
167
$query = clone $this->query;
168
return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db);
174
public function setSort($value)
176
parent::setSort($value);
177
if ($this->query instanceof ActiveQueryInterface && ($sort = $this->getSort()) !== false) {
178
/* @var $modelClass Model */
179
$modelClass = $this->query->modelClass;
180
$model = $modelClass::instance();
181
if (empty($sort->attributes)) {
182
foreach ($model->attributes() as $attribute) {
183
$sort->attributes[$attribute] = [
184
'asc' => [$attribute => SORT_ASC],
185
'desc' => [$attribute => SORT_DESC],
189
if ($sort->modelClass === null) {
190
$sort->modelClass = $modelClass;
195
public function __clone()
197
if (is_object($this->query)) {
198
$this->query = clone $this->query;