Commit b31b02a5 by Qiang Xue

renamed IDataProvider::getItems() to getModels()

parent 3d5388ff
...@@ -29,7 +29,7 @@ use yii\db\Connection; ...@@ -29,7 +29,7 @@ use yii\db\Connection;
* )); * ));
* *
* // get the posts in the current page * // get the posts in the current page
* $posts = $provider->getItems(); * $posts = $provider->getModels();
* ~~~ * ~~~
* *
* And the following example shows how to use ActiveDataProvider without ActiveRecord: * And the following example shows how to use ActiveDataProvider without ActiveRecord:
...@@ -44,7 +44,7 @@ use yii\db\Connection; ...@@ -44,7 +44,7 @@ use yii\db\Connection;
* )); * ));
* *
* // get the posts in the current page * // get the posts in the current page
* $posts = $provider->getItems(); * $posts = $provider->getModels();
* ~~~ * ~~~
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -53,18 +53,18 @@ use yii\db\Connection; ...@@ -53,18 +53,18 @@ use yii\db\Connection;
class ActiveDataProvider extends DataProvider class ActiveDataProvider extends DataProvider
{ {
/** /**
* @var Query the query that is used to fetch data items and [[totalCount]] * @var Query the query that is used to fetch data models and [[totalCount]]
* if it is not explicitly set. * if it is not explicitly set.
*/ */
public $query; public $query;
/** /**
* @var string|callable the column that is used as the key of the data items. * @var string|callable 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 item. * 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 following rules will be used to determine the keys of the data items: * If this is not set, the following rules will be used to determine the keys of the data models:
* *
* - If [[query]] is an [[ActiveQuery]] instance, the primary keys of [[ActiveQuery::modelClass]] will be used. * - 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. * - Otherwise, the keys of the [[models]] array will be used.
* *
* @see getKeys() * @see getKeys()
*/ */
...@@ -75,9 +75,9 @@ class ActiveDataProvider extends DataProvider ...@@ -75,9 +75,9 @@ class ActiveDataProvider extends DataProvider
*/ */
public $db; public $db;
private $_items; private $_models;
private $_keys; private $_keys;
private $_count; private $_totalCount;
/** /**
* Initializes the DbCache component. * Initializes the DbCache component.
...@@ -96,59 +96,59 @@ class ActiveDataProvider extends DataProvider ...@@ -96,59 +96,59 @@ class ActiveDataProvider extends DataProvider
} }
/** /**
* Returns the number of data items in the current page. * Returns the number of data models in the current page.
* This is equivalent to `count($provider->items)`. * This is equivalent to `count($provider->models)`.
* When [[pagination]] is false, this is the same as [[totalCount]]. * When [[pagination]] is false, this is the same as [[totalCount]].
* @param boolean $refresh whether to recalculate the item count. If true, * @param boolean $refresh whether to recalculate the model count. If true,
* this will cause re-fetching of [[items]]. * this will cause re-fetching of [[models]].
* @return integer the number of data items in the current page. * @return integer the number of data models in the current page.
*/ */
public function getCount($refresh = false) public function getCount($refresh = false)
{ {
return count($this->getItems($refresh)); return count($this->getModels($refresh));
} }
/** /**
* Returns the total number of data items. * Returns the total number of data models.
* When [[pagination]] is false, this returns the same value as [[count]]. * When [[pagination]] is false, this returns the same value as [[count]].
* If [[totalCount]] is not explicitly set, it will be calculated * If [[totalCount]] is not explicitly set, it will be calculated
* using [[query]] with a COUNT query. * using [[query]] with a COUNT query.
* @param boolean $refresh whether to recalculate the item count * @param boolean $refresh whether to recalculate the model count
* @return integer total number of possible data items. * @return integer total number of possible data models.
* @throws InvalidConfigException * @throws InvalidConfigException
*/ */
public function getTotalCount($refresh = false) public function getTotalCount($refresh = false)
{ {
if ($this->getPagination() === false) { if ($this->getPagination() === false) {
return $this->getCount($refresh); return $this->getCount($refresh);
} elseif ($this->_count === null || $refresh) { } elseif ($this->_totalCount === null || $refresh) {
if (!$this->query instanceof Query) { if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance of Query 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->db); $this->_totalCount = $query->limit(-1)->offset(-1)->count('*', $this->db);
} }
return $this->_count; return $this->_totalCount;
} }
/** /**
* Sets the total number of data items. * Sets the total number of data models.
* @param integer $value the total number of data items. * @param integer $value the total number of data models.
*/ */
public function setTotalCount($value) public function setTotalCount($value)
{ {
$this->_count = $value; $this->_totalCount = $value;
} }
/** /**
* Returns the data items in the current page. * Returns the data models in the current page.
* @param boolean $refresh whether to re-fetch the data items. * @param boolean $refresh whether to re-fetch the data models.
* @return array the list of data items in the current page. * @return array the list of data models in the current page.
* @throws InvalidConfigException * @throws InvalidConfigException
*/ */
public function getItems($refresh = false) public function getModels($refresh = false)
{ {
if ($this->_items === null || $refresh) { if ($this->_models === null || $refresh) {
if (!$this->query instanceof Query) { if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.'); throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.');
} }
...@@ -159,28 +159,28 @@ class ActiveDataProvider extends DataProvider ...@@ -159,28 +159,28 @@ class ActiveDataProvider extends DataProvider
if (($sort = $this->getSort()) !== false) { if (($sort = $this->getSort()) !== false) {
$this->query->orderBy($sort->getOrders()); $this->query->orderBy($sort->getOrders());
} }
$this->_items = $this->query->all($this->db); $this->_models = $this->query->all($this->db);
} }
return $this->_items; return $this->_models;
} }
/** /**
* Returns the key values associated with the data items. * Returns the key values associated with the data models.
* @param boolean $refresh whether to re-fetch the data items and re-calculate the keys * @param boolean $refresh whether to re-fetch the data models and re-calculate the keys
* @return array the list of key values corresponding to [[items]]. Each data item in [[items]] * @return 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. * is uniquely identified by the corresponding key value in this array.
*/ */
public function getKeys($refresh = false) public function getKeys($refresh = false)
{ {
if ($this->_keys === null || $refresh) { if ($this->_keys === null || $refresh) {
$this->_keys = array(); $this->_keys = array();
$items = $this->getItems($refresh); $models = $this->getModels($refresh);
if ($this->key !== null) { if ($this->key !== null) {
foreach ($items as $item) { foreach ($models as $model) {
if (is_string($this->key)) { if (is_string($this->key)) {
$this->_keys[] = $item[$this->key]; $this->_keys[] = $model[$this->key];
} else { } else {
$this->_keys[] = call_user_func($this->key, $item); $this->_keys[] = call_user_func($this->key, $model);
} }
} }
} elseif ($this->query instanceof ActiveQuery) { } elseif ($this->query instanceof ActiveQuery) {
...@@ -189,20 +189,20 @@ class ActiveDataProvider extends DataProvider ...@@ -189,20 +189,20 @@ class ActiveDataProvider extends DataProvider
$pks = $class::primaryKey(); $pks = $class::primaryKey();
if (count($pks) === 1) { if (count($pks) === 1) {
$pk = $pks[0]; $pk = $pks[0];
foreach ($items as $item) { foreach ($models as $model) {
$this->_keys[] = $item[$pk]; $this->_keys[] = $model[$pk];
} }
} else { } else {
foreach ($items as $item) { foreach ($models as $model) {
$keys = array(); $keys = array();
foreach ($pks as $pk) { foreach ($pks as $pk) {
$keys[] = $item[$pk]; $keys[] = $model[$pk];
} }
$this->_keys[] = json_encode($keys); $this->_keys[] = json_encode($keys);
} }
} }
} else { } else {
$this->_keys = array_keys($items); $this->_keys = array_keys($models);
} }
} }
return $this->_keys; return $this->_keys;
......
...@@ -13,25 +13,25 @@ use yii\helpers\ArrayHelper; ...@@ -13,25 +13,25 @@ use yii\helpers\ArrayHelper;
/** /**
* ArrayDataProvider implements a data provider based on a data array. * ArrayDataProvider implements a data provider based on a data array.
* *
* The [[allItems]] property contains all data items that may be sorted and/or paginated. * The [[allModels]] property contains all data models that may be sorted and/or paginated.
* ArrayDataProvider will provide the data after sorting and/or pagination. * ArrayDataProvider will provide the data after sorting and/or pagination.
* You may configure the [[sort]] and [[pagination]] properties to * You may configure the [[sort]] and [[pagination]] properties to
* customize the sorting and pagination behaviors. * customize the sorting and pagination behaviors.
* *
* Elements in the [[allItems]] array may be either objects (e.g. model objects) * Elements in the [[allModels]] array may be either objects (e.g. model objects)
* or associative arrays (e.g. query results of DAO). * or associative arrays (e.g. query results of DAO).
* Make sure to set the [[key]] property to the name of the field that uniquely * 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. * identifies a data record or false if you do not have such a field.
* *
* Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient * Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient
* because it needs to have [[allItems]] ready. * because it needs to have [[allModels]] ready.
* *
* ArrayDataProvider may be used in the following way: * ArrayDataProvider may be used in the following way:
* *
* ~~~ * ~~~
* $query = new Query; * $query = new Query;
* $provider = new ArrayDataProvider(array( * $provider = new ArrayDataProvider(array(
* 'allItems' => $query->from('tbl_post')->all(), * 'allModels' => $query->from('tbl_post')->all(),
* 'sort' => array( * 'sort' => array(
* 'attributes' => array( * 'attributes' => array(
* 'id', 'username', 'email', * 'id', 'username', 'email',
...@@ -42,7 +42,7 @@ use yii\helpers\ArrayHelper; ...@@ -42,7 +42,7 @@ use yii\helpers\ArrayHelper;
* ), * ),
* )); * ));
* // get the posts in the current page * // get the posts in the current page
* $posts = $provider->getItems(); * $posts = $provider->getModels();
* ~~~ * ~~~
* *
* Note: if you want to use the sorting feature, you must configure the [[sort]] property * Note: if you want to use the sorting feature, you must configure the [[sort]] property
...@@ -54,116 +54,110 @@ use yii\helpers\ArrayHelper; ...@@ -54,116 +54,110 @@ use yii\helpers\ArrayHelper;
class ArrayDataProvider extends DataProvider class ArrayDataProvider extends DataProvider
{ {
/** /**
* @var string|callable the column that is used as the key of the data items. * @var string|callable 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 item. * 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 [[items]] array will be used. * If this is not set, the index of the [[models]] array will be used.
* @see getKeys() * @see getKeys()
*/ */
public $key; public $key;
/** /**
* @var array the data that is not paginated or sorted. When pagination is enabled, * @var array the data that is not paginated or sorted. When pagination is enabled,
* this property usually contains more elements than [[items]]. * this property usually contains more elements than [[models]].
* The array elements must use zero-based integer keys. * The array elements must use zero-based integer keys.
*/ */
public $allItems; public $allModels;
private $_totalCount; private $_totalCount;
/** /**
* Returns the total number of data items. * Returns the total number of data models.
* @return integer total number of possible data items. * @return integer total number of possible data models.
* @throws InvalidConfigException
*/ */
public function getTotalCount() public function getTotalCount()
{ {
if ($this->getPagination() === false) { if ($this->getPagination() === false) {
return $this->getCount(); return $this->getCount();
} elseif ($this->_totalCount === null) { } elseif ($this->_totalCount === null) {
if ($this->allItems !== null) { $this->_totalCount = count($this->allModels);
$this->_totalCount = count($this->allItems);
} else {
throw new InvalidConfigException('Unable to determine total item count: either "allItems" or "totalCount" must be set.');
}
} }
return $this->_totalCount; return $this->_totalCount;
} }
/** /**
* Sets the total number of data items. * Sets the total number of data models.
* @param integer $value the total number of data items. * @param integer $value the total number of data models.
*/ */
public function setTotalCount($value) public function setTotalCount($value)
{ {
$this->_totalCount = $value; $this->_totalCount = $value;
} }
private $_items; private $_models;
/** /**
* Returns the data items in the current page. * Returns the data models in the current page.
* @return array the list of data items in the current page. * @return array the list of data models in the current page.
* @throws InvalidConfigException
*/ */
public function getItems() public function getModels()
{ {
if ($this->_items === null) { if ($this->_models === null) {
if (($items = $this->allItems) === null) { if (($models = $this->allModels) === null) {
throw new InvalidConfigException('Either "items" or "allItems" must be set.'); return array();
} }
if (($sort = $this->getSort()) !== false) { if (($sort = $this->getSort()) !== false) {
$items = $this->sortItems($items, $sort); $models = $this->sortModels($models, $sort);
} }
if (($pagination = $this->getPagination()) !== false) { if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount(); $pagination->totalCount = $this->getTotalCount();
$items = array_slice($items, $pagination->getOffset(), $pagination->getLimit()); $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit());
} }
$this->_items = $items; $this->_models = $models;
} }
return $this->_items; return $this->_models;
} }
/** /**
* Sets the data items in the current page. * Sets the data models in the current page.
* @param array $items the items in the current page * @param array $models the models in the current page
*/ */
public function setItems($items) public function setModels($models)
{ {
$this->_items = $items; $this->_models = $models;
} }
private $_keys; private $_keys;
/** /**
* Returns the key values associated with the data items. * Returns the key values associated with the data models.
* @return array the list of key values corresponding to [[items]]. Each data item in [[items]] * @return 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. * is uniquely identified by the corresponding key value in this array.
*/ */
public function getKeys() public function getKeys()
{ {
if ($this->_keys === null) { if ($this->_keys === null) {
$this->_keys = array(); $this->_keys = array();
$items = $this->getItems(); $models = $this->getModels();
if ($this->key !== null) { if ($this->key !== null) {
foreach ($items as $item) { foreach ($models as $model) {
if (is_string($this->key)) { if (is_string($this->key)) {
$this->_keys[] = $item[$this->key]; $this->_keys[] = $model[$this->key];
} else { } else {
$this->_keys[] = call_user_func($this->key, $item); $this->_keys[] = call_user_func($this->key, $model);
} }
} }
} else { } else {
$this->_keys = array_keys($items); $this->_keys = array_keys($models);
} }
} }
return $this->_keys; return $this->_keys;
} }
/** /**
* Sets the key values associated with the data items. * Sets the key values associated with the data models.
* @param array $keys the list of key values corresponding to [[items]]. * @param array $keys the list of key values corresponding to [[models]].
*/ */
public function setKeys($keys) public function setKeys($keys)
{ {
...@@ -171,17 +165,17 @@ class ArrayDataProvider extends DataProvider ...@@ -171,17 +165,17 @@ class ArrayDataProvider extends DataProvider
} }
/** /**
* Sorts the data items according to the given sort definition * Sorts the data models according to the given sort definition
* @param array $items the items to be sorted * @param array $models the models to be sorted
* @param Sort $sort the sort definition * @param Sort $sort the sort definition
* @return array the sorted data items * @return array the sorted data models
*/ */
protected function sortItems($items, $sort) protected function sortModels($models, $sort)
{ {
$orders = $sort->getOrders(); $orders = $sort->getOrders();
if (!empty($orders)) { if (!empty($orders)) {
ArrayHelper::multisort($items, array_keys($orders), array_values($orders)); ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
} }
return $items; return $models;
} }
} }
...@@ -118,11 +118,11 @@ abstract class DataProvider extends Component implements IDataProvider ...@@ -118,11 +118,11 @@ abstract class DataProvider extends Component implements IDataProvider
} }
/** /**
* Returns the number of data items in the current page. * Returns the number of data models in the current page.
* @return integer the number of data items in the current page. * @return integer the number of data models in the current page.
*/ */
public function getCount() public function getCount()
{ {
return count($this->getItems()); return count($this->getModels());
} }
} }
...@@ -19,29 +19,29 @@ namespace yii\data; ...@@ -19,29 +19,29 @@ namespace yii\data;
interface IDataProvider interface IDataProvider
{ {
/** /**
* Returns the number of data items in the current page. * Returns the number of data models in the current page.
* This is equivalent to `count($provider->getItems())`. * This is equivalent to `count($provider->getModels())`.
* When [[pagination]] is false, this is the same as [[totalCount]]. * When [[pagination]] is false, this is the same as [[totalCount]].
* @return integer the number of data items in the current page. * @return integer the number of data models in the current page.
*/ */
public function getCount(); public function getCount();
/** /**
* Returns the total number of data items. * Returns the total number of data models.
* When [[pagination]] is false, this is the same as [[count]]. * When [[pagination]] is false, this is the same as [[count]].
* @return integer total number of possible data items. * @return integer total number of possible data models.
*/ */
public function getTotalCount(); public function getTotalCount();
/** /**
* Returns the data items in the current page. * Returns the data models in the current page.
* @return array the list of data items in the current page. * @return array the list of data models in the current page.
*/ */
public function getItems(); public function getModels();
/** /**
* Returns the key values associated with the data items. * Returns the key values associated with the data models.
* @return array the list of key values corresponding to [[items]]. Each data item in [[items]] * @return 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. * is uniquely identified by the corresponding key value in this array.
*/ */
public function getKeys(); public function getKeys();
......
...@@ -112,7 +112,7 @@ class GridView extends ListViewBase ...@@ -112,7 +112,7 @@ class GridView extends ListViewBase
} }
/** /**
* Renders the data items for the grid view. * Renders the data models for the grid view.
*/ */
public function renderItems() public function renderItems()
{ {
...@@ -217,22 +217,22 @@ class GridView extends ListViewBase ...@@ -217,22 +217,22 @@ class GridView extends ListViewBase
*/ */
public function renderTableBody() public function renderTableBody()
{ {
$items = array_values($this->dataProvider->getItems()); $models = array_values($this->dataProvider->getModels());
$keys = $this->dataProvider->getKeys(); $keys = $this->dataProvider->getKeys();
$rows = array(); $rows = array();
foreach ($items as $index => $item) { foreach ($models as $index => $model) {
$key = $keys[$index]; $key = $keys[$index];
if ($this->beforeRow !== null) { if ($this->beforeRow !== null) {
$row = call_user_func($this->beforeRow, $item, $key, $index); $row = call_user_func($this->beforeRow, $model, $key, $index);
if (!empty($row)) { if (!empty($row)) {
$rows[] = $row; $rows[] = $row;
} }
} }
$rows[] = $this->renderTableRow($item, $key, $index); $rows[] = $this->renderTableRow($model, $key, $index);
if ($this->afterRow !== null) { if ($this->afterRow !== null) {
$row = call_user_func($this->afterRow, $item, $key, $index); $row = call_user_func($this->afterRow, $model, $key, $index);
if (!empty($row)) { if (!empty($row)) {
$rows[] = $row; $rows[] = $row;
} }
...@@ -242,21 +242,21 @@ class GridView extends ListViewBase ...@@ -242,21 +242,21 @@ class GridView extends ListViewBase
} }
/** /**
* Renders a table row with the given data item and key. * Renders a table row with the given data model and key.
* @param mixed $item the data item * @param mixed $model the data model to be rendered
* @param mixed $key the key associated with the data item * @param mixed $key the key associated with the data model
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]]. * @param integer $index the zero-based index of the data model among the model array returned by [[dataProvider]].
* @return string the rendering result * @return string the rendering result
*/ */
public function renderTableRow($item, $key, $index) public function renderTableRow($model, $key, $index)
{ {
$cells = array(); $cells = array();
/** @var \yii\widgets\grid\Column $column */ /** @var \yii\widgets\grid\Column $column */
foreach ($this->columns as $column) { foreach ($this->columns as $column) {
$cells[] = $column->renderDataCell($item, $index); $cells[] = $column->renderDataCell($model, $index);
} }
if ($this->rowOptions instanceof Closure) { if ($this->rowOptions instanceof Closure) {
$options = call_user_func($this->rowOptions, $item, $key, $index); $options = call_user_func($this->rowOptions, $model, $key, $index);
} else { } else {
$options = $this->rowOptions; $options = $this->rowOptions;
} }
...@@ -315,10 +315,10 @@ class GridView extends ListViewBase ...@@ -315,10 +315,10 @@ class GridView extends ListViewBase
protected function guessColumns() protected function guessColumns()
{ {
$items = $this->dataProvider->getItems(); $models = $this->dataProvider->getModels();
$item = reset($items); $model = reset($models);
if (is_array($item) || is_object($item)) { if (is_array($model) || is_object($model)) {
foreach ($item as $name => $value) { foreach ($model as $name => $value) {
$this->columns[] = $name; $this->columns[] = $name;
} }
} else { } else {
......
...@@ -19,7 +19,7 @@ use yii\helpers\Html; ...@@ -19,7 +19,7 @@ use yii\helpers\Html;
class ListView extends ListViewBase class ListView extends ListViewBase
{ {
/** /**
* @var array the HTML attributes for the container of the rendering result of each data item. * @var array the HTML attributes for the container of the rendering result of each data model.
* The "tag" element specifies the tag name of the container element and defaults to "div". * The "tag" element specifies the tag name of the container element and defaults to "div".
* If "tag" is false, it means no container element will be rendered. * If "tag" is false, it means no container element will be rendered.
*/ */
...@@ -29,7 +29,7 @@ class ListView extends ListViewBase ...@@ -29,7 +29,7 @@ class ListView extends ListViewBase
* for rendering each data item. If it specifies a view name, the following variables will * for rendering each data item. If it specifies a view name, the following variables will
* be available in the view: * be available in the view:
* *
* - `$item`: mixed, the data item * - `$model`: mixed, the data model
* - `$key`: mixed, the key value associated with the data item * - `$key`: mixed, the key value associated with the data item
* - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]]. * - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
* - `$widget`: ListView, this widget instance * - `$widget`: ListView, this widget instance
...@@ -39,7 +39,7 @@ class ListView extends ListViewBase ...@@ -39,7 +39,7 @@ class ListView extends ListViewBase
* If this property is specified as a callback, it should have the following signature: * If this property is specified as a callback, it should have the following signature:
* *
* ~~~ * ~~~
* function ($item, $key, $index, $widget) * function ($model, $key, $index, $widget)
* ~~~ * ~~~
*/ */
public $itemView; public $itemView;
...@@ -50,40 +50,40 @@ class ListView extends ListViewBase ...@@ -50,40 +50,40 @@ class ListView extends ListViewBase
/** /**
* Renders all data items. * Renders all data models.
* @return string the rendering result * @return string the rendering result
*/ */
public function renderItems() public function renderItems()
{ {
$items = $this->dataProvider->getItems(); $models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys(); $keys = $this->dataProvider->getKeys();
$rows = array(); $rows = array();
foreach (array_values($items) as $index => $item) { foreach (array_values($models) as $index => $model) {
$rows[] = $this->renderItem($item, $keys[$index], $index); $rows[] = $this->renderItem($model, $keys[$index], $index);
} }
return implode($this->separator, $rows); return implode($this->separator, $rows);
} }
/** /**
* Renders a single data item. * Renders a single data model.
* @param mixed $item the data item to be rendered * @param mixed $model the data model to be rendered
* @param mixed $key the key value associated with the data item * @param mixed $key the key value associated with the data model
* @param integer $index the zero-based index of the data item in the item array returned by [[dataProvider]]. * @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
* @return string the rendering result * @return string the rendering result
*/ */
public function renderItem($item, $key, $index) public function renderItem($model, $key, $index)
{ {
if ($this->itemView === null) { if ($this->itemView === null) {
$content = $key; $content = $key;
} elseif (is_string($this->itemView)) { } elseif (is_string($this->itemView)) {
$content = $this->getView()->render($this->itemView, array( $content = $this->getView()->render($this->itemView, array(
'item' => $item, 'model' => $model,
'key' => $key, 'key' => $key,
'index' => $index, 'index' => $index,
'widget' => $this, 'widget' => $this,
)); ));
} else { } else {
$content = call_user_func($this->itemView, $item, $key, $index, $this); $content = call_user_func($this->itemView, $model, $key, $index, $this);
} }
$options = $this->itemOptions; $options = $this->itemOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div'); $tag = ArrayHelper::remove($options, 'tag', 'div');
......
...@@ -70,7 +70,7 @@ abstract class ListViewBase extends Widget ...@@ -70,7 +70,7 @@ abstract class ListViewBase extends Widget
/** /**
* Renders the data items. * Renders the data models.
* @return string the rendering result. * @return string the rendering result.
*/ */
abstract public function renderItems(); abstract public function renderItems();
......
...@@ -75,18 +75,18 @@ class Column extends Object ...@@ -75,18 +75,18 @@ class Column extends Object
/** /**
* Renders a data cell. * Renders a data cell.
* @param mixed $item the data item * @param mixed $model the data model being rendered
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]]. * @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]].
* @return string the rendering result * @return string the rendering result
*/ */
public function renderDataCell($item, $index) public function renderDataCell($model, $index)
{ {
if ($this->bodyOptions instanceof Closure) { if ($this->bodyOptions instanceof Closure) {
$options = call_user_func($this->bodyOptions, $item, $index, $this); $options = call_user_func($this->bodyOptions, $model, $index, $this);
} else { } else {
$options = $this->bodyOptions; $options = $this->bodyOptions;
} }
return Html::tag('td', $this->renderDataCellContent($item, $index), $options); return Html::tag('td', $this->renderDataCellContent($model, $index), $options);
} }
/** /**
...@@ -121,14 +121,14 @@ class Column extends Object ...@@ -121,14 +121,14 @@ class Column extends Object
/** /**
* Renders the data cell content. * Renders the data cell content.
* @param mixed $item the data item * @param mixed $model the data model
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]]. * @param integer $index the zero-based index of the data model among the models array returned by [[dataProvider]].
* @return string the rendering result * @return string the rendering result
*/ */
protected function renderDataCellContent($item, $index) protected function renderDataCellContent($model, $index)
{ {
if ($this->content !== null) { if ($this->content !== null) {
return call_user_func($this->content, $item, $index, $this); return call_user_func($this->content, $model, $index, $this);
} else { } else {
return $this->grid->emptyCell; return $this->grid->emptyCell;
} }
......
...@@ -37,7 +37,6 @@ class DataColumn extends Column ...@@ -37,7 +37,6 @@ class DataColumn extends Column
* If this property is an array, a dropdown list will be generated that uses this property value as * If this property is an array, a dropdown list will be generated that uses this property value as
* the list options. * the list options.
* If you don't want a filter for this data column, set this value to false. * If you don't want a filter for this data column, set this value to false.
* @since 1.1.1
*/ */
public $filter; public $filter;
...@@ -49,10 +48,10 @@ class DataColumn extends Column ...@@ -49,10 +48,10 @@ class DataColumn extends Column
if ($this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { if ($this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
return $sort->link($this->attribute); return $sort->link($this->attribute);
} }
$items = $provider->getItems(); $models = $provider->getModels();
if (($item = reset($items)) instanceof Model) { if (($model = reset($models)) instanceof Model) {
/** @var Model $item */ /** @var Model $model */
return $item->getAttributeLabel($this->attribute); return $model->getAttributeLabel($this->attribute);
} elseif ($provider instanceof ActiveDataProvider) { } elseif ($provider instanceof ActiveDataProvider) {
if ($provider->query instanceof ActiveQuery) { if ($provider->query instanceof ActiveQuery) {
/** @var Model $model */ /** @var Model $model */
...@@ -81,14 +80,14 @@ class DataColumn extends Column ...@@ -81,14 +80,14 @@ class DataColumn extends Column
} }
} }
protected function renderDataCellContent($item, $index) protected function renderDataCellContent($model, $index)
{ {
if ($this->value !== null) { if ($this->value !== null) {
$value = call_user_func($this->value, $item, $index, $this); $value = call_user_func($this->value, $model, $index, $this);
} elseif ($this->content === null && $this->attribute !== null) { } elseif ($this->content === null && $this->attribute !== null) {
$value = ArrayHelper::getValue($item, $this->attribute); $value = ArrayHelper::getValue($model, $this->attribute);
} else { } else {
return parent::renderDataCellContent($item, $index); return parent::renderDataCellContent($model, $index);
} }
return $this->grid->formatter->format($value, $this->format); return $this->grid->formatter->format($value, $this->format);
} }
......
...@@ -30,7 +30,7 @@ class ActiveDataProviderTest extends DatabaseTestCase ...@@ -30,7 +30,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
$provider = new ActiveDataProvider(array( $provider = new ActiveDataProvider(array(
'query' => Order::find()->orderBy('id'), 'query' => Order::find()->orderBy('id'),
)); ));
$orders = $provider->getItems(); $orders = $provider->getModels();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertTrue($orders[0] instanceof Order); $this->assertTrue($orders[0] instanceof Order);
$this->assertEquals(array(1, 2, 3), $provider->getKeys()); $this->assertEquals(array(1, 2, 3), $provider->getKeys());
...@@ -41,7 +41,7 @@ class ActiveDataProviderTest extends DatabaseTestCase ...@@ -41,7 +41,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'pageSize' => 2, 'pageSize' => 2,
) )
)); ));
$orders = $provider->getItems(); $orders = $provider->getModels();
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
} }
...@@ -52,7 +52,7 @@ class ActiveDataProviderTest extends DatabaseTestCase ...@@ -52,7 +52,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'db' => $this->getConnection(), 'db' => $this->getConnection(),
'query' => $query->from('tbl_order')->orderBy('id'), 'query' => $query->from('tbl_order')->orderBy('id'),
)); ));
$orders = $provider->getItems(); $orders = $provider->getModels();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertTrue(is_array($orders[0])); $this->assertTrue(is_array($orders[0]));
$this->assertEquals(array(0, 1, 2), $provider->getKeys()); $this->assertEquals(array(0, 1, 2), $provider->getKeys());
...@@ -65,7 +65,7 @@ class ActiveDataProviderTest extends DatabaseTestCase ...@@ -65,7 +65,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'pageSize' => 2, 'pageSize' => 2,
) )
)); ));
$orders = $provider->getItems(); $orders = $provider->getModels();
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
} }
} }
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