Commit ad2d1881 by Qiang Xue

Refactored ActiveDataProvider.

parent 19e938ce
...@@ -8,13 +8,13 @@ ...@@ -8,13 +8,13 @@
namespace yii\data; namespace yii\data;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Query;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
/** /**
* ActiveDataProvider implements a data provider based on [[ActiveQuery]]. * ActiveDataProvider implements a data provider based on [[Query]] and [[ActiveQuery]].
* *
* ActiveDataProvider provides data in terms of [[ActiveRecord]] objects. It uses * ActiveDataProvider provides data by performing DB queries using [[query]].
* [[query]] to fetch the data items in a sorted and paginated manner.
* *
* The following is an example of using ActiveDataProvider: * The following is an example of using ActiveDataProvider:
* *
...@@ -36,16 +36,22 @@ use yii\db\ActiveQuery; ...@@ -36,16 +36,22 @@ use yii\db\ActiveQuery;
class ActiveDataProvider extends DataProvider class ActiveDataProvider extends DataProvider
{ {
/** /**
* @var ActiveQuery the query that is used to fetch data items and [[totalItemCount]] * @var Query the query that is used to fetch data items and [[totalItemCount]]
* if it is not explicitly set. * if it is not explicitly set.
*/ */
public $query; public $query;
/** /**
* @var string|callable the attribute that is used as the key of the data items. * @var string|callable the column that is used as the key of the data items.
* This can be either the name of the attribute, or a callable that returns the key value * This can be either a column name, or a callable that returns the key value of a given data item.
* of a given data item. If not set, the primary key of [[ActiveQuery::modelClass]] will be used. *
* If this is not set, the following rules will be used to determine the keys of the data items:
*
* - If [[query]] is an [[ActiveQuery]] instance, the primary keys of [[ActiveQuery::modelClass]] will be used.
* - Otherwise, the keys of the [[items]] array will be used.
*
* @see getKeys()
*/ */
public $keyAttribute; public $key;
private $_items; private $_items;
private $_keys; private $_keys;
...@@ -53,7 +59,7 @@ class ActiveDataProvider extends DataProvider ...@@ -53,7 +59,7 @@ class ActiveDataProvider extends DataProvider
/** /**
* Returns the number of data items in the current page. * Returns the number of data items in the current page.
* This is equivalent to `count($provider->getItems())`. * This is equivalent to `count($provider->items)`.
* When [[pagination]] is false, this is the same as [[totalItemCount]]. * When [[pagination]] is false, this is the same as [[totalItemCount]].
* @param boolean $refresh whether to recalculate the item count. If true, * @param boolean $refresh whether to recalculate the item count. If true,
* this will cause re-fetching of [[items]]. * this will cause re-fetching of [[items]].
...@@ -78,8 +84,8 @@ class ActiveDataProvider extends DataProvider ...@@ -78,8 +84,8 @@ class ActiveDataProvider extends DataProvider
if ($this->getPagination() === false) { if ($this->getPagination() === false) {
return $this->getItemCount($refresh); return $this->getItemCount($refresh);
} elseif ($this->_count === null || $refresh) { } elseif ($this->_count === null || $refresh) {
if (!$this->query instanceof ActiveQuery) { if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance of ActiveQuery or its subclass.'); throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.');
} }
$query = clone $this->query; $query = clone $this->query;
$this->_count = $query->limit(-1)->offset(-1)->count(); $this->_count = $query->limit(-1)->offset(-1)->count();
...@@ -105,8 +111,8 @@ class ActiveDataProvider extends DataProvider ...@@ -105,8 +111,8 @@ class ActiveDataProvider extends DataProvider
public function getItems($refresh = false) public function getItems($refresh = false)
{ {
if ($this->_items === null || $refresh) { if ($this->_items === null || $refresh) {
if (!$this->query instanceof ActiveQuery) { if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance of ActiveQuery or its subclass.'); throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.');
} }
if (($pagination = $this->getPagination()) !== false) { if (($pagination = $this->getPagination()) !== false) {
$pagination->itemCount = $this->getTotalItemCount(); $pagination->itemCount = $this->getTotalItemCount();
...@@ -131,8 +137,15 @@ class ActiveDataProvider extends DataProvider ...@@ -131,8 +137,15 @@ class ActiveDataProvider extends DataProvider
if ($this->_keys === null || $refresh) { if ($this->_keys === null || $refresh) {
$this->_keys = array(); $this->_keys = array();
$items = $this->getItems($refresh); $items = $this->getItems($refresh);
$keyAttribute = $this->keyAttribute; if ($this->key !== null) {
if ($keyAttribute === null) { foreach ($items as $item) {
if (is_string($this->key)) {
$this->_keys[] = $item[$this->key];
} else {
$this->_keys[] = call_user_func($this->key, $item);
}
}
} elseif ($this->query instanceof ActiveQuery) {
/** @var \yii\db\ActiveRecord $class */ /** @var \yii\db\ActiveRecord $class */
$class = $this->query->modelClass; $class = $this->query->modelClass;
$pks = $class::primaryKey(); $pks = $class::primaryKey();
...@@ -151,13 +164,7 @@ class ActiveDataProvider extends DataProvider ...@@ -151,13 +164,7 @@ class ActiveDataProvider extends DataProvider
} }
} }
} else { } else {
foreach ($items as $item) { $this->_keys = array_keys($items);
if (is_string($this->keyAttribute)) {
$this->_keys[] = $item[$this->keyAttribute];
} else {
$this->_keys[] = call_user_func($item, $this->keyAttribute);
}
}
} }
} }
return $this->_keys; return $this->_keys;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment