Browse Source

renamed IDataProvider::getItems() to getModels()

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
b31b02a55d
  1. 86
      framework/yii/data/ActiveDataProvider.php
  2. 96
      framework/yii/data/ArrayDataProvider.php
  3. 6
      framework/yii/data/DataProvider.php
  4. 20
      framework/yii/data/IDataProvider.php
  5. 34
      framework/yii/widgets/GridView.php
  6. 28
      framework/yii/widgets/ListView.php
  7. 2
      framework/yii/widgets/ListViewBase.php
  8. 16
      framework/yii/widgets/grid/Column.php
  9. 17
      framework/yii/widgets/grid/DataColumn.php
  10. 8
      tests/unit/framework/data/ActiveDataProviderTest.php

86
framework/yii/data/ActiveDataProvider.php

@ -29,7 +29,7 @@ use yii\db\Connection;
* ));
*
* // get the posts in the current page
* $posts = $provider->getItems();
* $posts = $provider->getModels();
* ~~~
*
* And the following example shows how to use ActiveDataProvider without ActiveRecord:
@ -44,7 +44,7 @@ use yii\db\Connection;
* ));
*
* // get the posts in the current page
* $posts = $provider->getItems();
* $posts = $provider->getModels();
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
@ -53,18 +53,18 @@ use yii\db\Connection;
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.
*/
public $query;
/**
* @var string|callable the column that is used as the key of the data items.
* This can be either a column name, or a callable that returns the key value of a given data item.
* @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 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.
* - Otherwise, the keys of the [[items]] array will be used.
* - Otherwise, the keys of the [[models]] array will be used.
*
* @see getKeys()
*/
@ -75,9 +75,9 @@ class ActiveDataProvider extends DataProvider
*/
public $db;
private $_items;
private $_models;
private $_keys;
private $_count;
private $_totalCount;
/**
* Initializes the DbCache component.
@ -96,59 +96,59 @@ class ActiveDataProvider extends DataProvider
}
/**
* Returns the number of data items in the current page.
* This is equivalent to `count($provider->items)`.
* Returns the number of data models in the current page.
* This is equivalent to `count($provider->models)`.
* When [[pagination]] is false, this is the same as [[totalCount]].
* @param boolean $refresh whether to recalculate the item count. If true,
* this will cause re-fetching of [[items]].
* @return integer the number of data items in the current page.
* @param boolean $refresh whether to recalculate the model count. If true,
* this will cause re-fetching of [[models]].
* @return integer the number of data models in the current page.
*/
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]].
* If [[totalCount]] is not explicitly set, it will be calculated
* using [[query]] with a COUNT query.
* @param boolean $refresh whether to recalculate the item count
* @return integer total number of possible data items.
* @param boolean $refresh whether to recalculate the model count
* @return integer total number of possible data models.
* @throws InvalidConfigException
*/
public function getTotalCount($refresh = false)
{
if ($this->getPagination() === false) {
return $this->getCount($refresh);
} elseif ($this->_count === null || $refresh) {
} elseif ($this->_totalCount === null || $refresh) {
if (!$this->query instanceof Query) {
throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.');
}
$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.
* @param integer $value the total number of data items.
* Sets the total number of data models.
* @param integer $value the total number of data models.
*/
public function setTotalCount($value)
{
$this->_count = $value;
$this->_totalCount = $value;
}
/**
* Returns the data items in the current page.
* @param boolean $refresh whether to re-fetch the data items.
* @return array the list of data items in the current page.
* Returns the data models in the current page.
* @param boolean $refresh whether to re-fetch the data models.
* @return array the list of data models in the current page.
* @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) {
throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.');
}
@ -159,28 +159,28 @@ class ActiveDataProvider extends DataProvider
if (($sort = $this->getSort()) !== false) {
$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.
* @param boolean $refresh whether to re-fetch the data items and re-calculate the keys
* @return array the list of key values corresponding to [[items]]. Each data item in [[items]]
* Returns the key values associated with the data models.
* @param boolean $refresh whether to re-fetch the data models and re-calculate the keys
* @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.
*/
public function getKeys($refresh = false)
{
if ($this->_keys === null || $refresh) {
$this->_keys = array();
$items = $this->getItems($refresh);
$models = $this->getModels($refresh);
if ($this->key !== null) {
foreach ($items as $item) {
foreach ($models as $model) {
if (is_string($this->key)) {
$this->_keys[] = $item[$this->key];
$this->_keys[] = $model[$this->key];
} else {
$this->_keys[] = call_user_func($this->key, $item);
$this->_keys[] = call_user_func($this->key, $model);
}
}
} elseif ($this->query instanceof ActiveQuery) {
@ -189,20 +189,20 @@ class ActiveDataProvider extends DataProvider
$pks = $class::primaryKey();
if (count($pks) === 1) {
$pk = $pks[0];
foreach ($items as $item) {
$this->_keys[] = $item[$pk];
foreach ($models as $model) {
$this->_keys[] = $model[$pk];
}
} else {
foreach ($items as $item) {
foreach ($models as $model) {
$keys = array();
foreach ($pks as $pk) {
$keys[] = $item[$pk];
$keys[] = $model[$pk];
}
$this->_keys[] = json_encode($keys);
}
}
} else {
$this->_keys = array_keys($items);
$this->_keys = array_keys($models);
}
}
return $this->_keys;

96
framework/yii/data/ArrayDataProvider.php

@ -13,25 +13,25 @@ use yii\helpers\ArrayHelper;
/**
* 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.
* You may configure the [[sort]] and [[pagination]] properties to
* 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).
* 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 [[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:
*
* ~~~
* $query = new Query;
* $provider = new ArrayDataProvider(array(
* 'allItems' => $query->from('tbl_post')->all(),
* 'allModels' => $query->from('tbl_post')->all(),
* 'sort' => array(
* 'attributes' => array(
* 'id', 'username', 'email',
@ -42,7 +42,7 @@ use yii\helpers\ArrayHelper;
* ),
* ));
* // 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
@ -54,116 +54,110 @@ use yii\helpers\ArrayHelper;
class ArrayDataProvider extends DataProvider
{
/**
* @var string|callable the column that is used as the key of the data items.
* This can be either a column name, or a callable that returns the key value of a given data item.
* If this is not set, the index of the [[items]] array will be used.
* @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 model.
* If this is not set, the index of the [[models]] array will be used.
* @see getKeys()
*/
public $key;
/**
* @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.
*/
public $allItems;
public $allModels;
private $_totalCount;
/**
* Returns the total number of data items.
* @return integer total number of possible data items.
* @throws InvalidConfigException
* Returns the total number of data models.
* @return integer total number of possible data models.
*/
public function getTotalCount()
{
if ($this->getPagination() === false) {
return $this->getCount();
} elseif ($this->_totalCount === null) {
if ($this->allItems !== null) {
$this->_totalCount = count($this->allItems);
} else {
throw new InvalidConfigException('Unable to determine total item count: either "allItems" or "totalCount" must be set.');
}
$this->_totalCount = count($this->allModels);
}
return $this->_totalCount;
}
/**
* Sets the total number of data items.
* @param integer $value the total number of data items.
* Sets the total number of data models.
* @param integer $value the total number of data models.
*/
public function setTotalCount($value)
{
$this->_totalCount = $value;
}
private $_items;
private $_models;
/**
* Returns the data items in the current page.
* @return array the list of data items in the current page.
* @throws InvalidConfigException
* Returns the data models in the current page.
* @return array the list of data models in the current page.
*/
public function getItems()
public function getModels()
{
if ($this->_items === null) {
if (($items = $this->allItems) === null) {
throw new InvalidConfigException('Either "items" or "allItems" must be set.');
if ($this->_models === null) {
if (($models = $this->allModels) === null) {
return array();
}
if (($sort = $this->getSort()) !== false) {
$items = $this->sortItems($items, $sort);
$models = $this->sortModels($models, $sort);
}
if (($pagination = $this->getPagination()) !== false) {
$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.
* @param array $items the items in the current page
* Sets the data models 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;
/**
* Returns the key values associated with the data items.
* @return array the list of key values corresponding to [[items]]. Each data item in [[items]]
* Returns the key values associated with the data models.
* @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.
*/
public function getKeys()
{
if ($this->_keys === null) {
$this->_keys = array();
$items = $this->getItems();
$models = $this->getModels();
if ($this->key !== null) {
foreach ($items as $item) {
foreach ($models as $model) {
if (is_string($this->key)) {
$this->_keys[] = $item[$this->key];
$this->_keys[] = $model[$this->key];
} else {
$this->_keys[] = call_user_func($this->key, $item);
$this->_keys[] = call_user_func($this->key, $model);
}
}
} else {
$this->_keys = array_keys($items);
$this->_keys = array_keys($models);
}
}
return $this->_keys;
}
/**
* Sets the key values associated with the data items.
* @param array $keys the list of key values corresponding to [[items]].
* Sets the key values associated with the data models.
* @param array $keys the list of key values corresponding to [[models]].
*/
public function setKeys($keys)
{
@ -171,17 +165,17 @@ class ArrayDataProvider extends DataProvider
}
/**
* Sorts the data items according to the given sort definition
* @param array $items the items to be sorted
* Sorts the data models according to the given sort definition
* @param array $models the models to be sorted
* @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();
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;
}
}

6
framework/yii/data/DataProvider.php

@ -118,11 +118,11 @@ abstract class DataProvider extends Component implements IDataProvider
}
/**
* Returns the number of data items in the current page.
* @return integer 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 models in the current page.
*/
public function getCount()
{
return count($this->getItems());
return count($this->getModels());
}
}

20
framework/yii/data/IDataProvider.php

@ -19,29 +19,29 @@ namespace yii\data;
interface IDataProvider
{
/**
* Returns the number of data items in the current page.
* This is equivalent to `count($provider->getItems())`.
* Returns the number of data models in the current page.
* This is equivalent to `count($provider->getModels())`.
* 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();
/**
* Returns the total number of data items.
* Returns the total number of data models.
* 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();
/**
* Returns the data items in the current page.
* @return array the list of data items in the current page.
* Returns the data models 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.
* @return array the list of key values corresponding to [[items]]. Each data item in [[items]]
* Returns the key values associated with the data models.
* @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.
*/
public function getKeys();

34
framework/yii/widgets/GridView.php

@ -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()
{
@ -217,22 +217,22 @@ class GridView extends ListViewBase
*/
public function renderTableBody()
{
$items = array_values($this->dataProvider->getItems());
$models = array_values($this->dataProvider->getModels());
$keys = $this->dataProvider->getKeys();
$rows = array();
foreach ($items as $index => $item) {
foreach ($models as $index => $model) {
$key = $keys[$index];
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)) {
$rows[] = $row;
}
}
$rows[] = $this->renderTableRow($item, $key, $index);
$rows[] = $this->renderTableRow($model, $key, $index);
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)) {
$rows[] = $row;
}
@ -242,21 +242,21 @@ class GridView extends ListViewBase
}
/**
* Renders a table row with the given data item and key.
* @param mixed $item the data item
* @param mixed $key the key associated with the data item
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]].
* Renders a table row with the given data model and key.
* @param mixed $model the data model to be rendered
* @param mixed $key the key associated with the data model
* @param integer $index the zero-based index of the data model among the model array returned by [[dataProvider]].
* @return string the rendering result
*/
public function renderTableRow($item, $key, $index)
public function renderTableRow($model, $key, $index)
{
$cells = array();
/** @var \yii\widgets\grid\Column $column */
foreach ($this->columns as $column) {
$cells[] = $column->renderDataCell($item, $index);
$cells[] = $column->renderDataCell($model, $index);
}
if ($this->rowOptions instanceof Closure) {
$options = call_user_func($this->rowOptions, $item, $key, $index);
$options = call_user_func($this->rowOptions, $model, $key, $index);
} else {
$options = $this->rowOptions;
}
@ -315,10 +315,10 @@ class GridView extends ListViewBase
protected function guessColumns()
{
$items = $this->dataProvider->getItems();
$item = reset($items);
if (is_array($item) || is_object($item)) {
foreach ($item as $name => $value) {
$models = $this->dataProvider->getModels();
$model = reset($models);
if (is_array($model) || is_object($model)) {
foreach ($model as $name => $value) {
$this->columns[] = $name;
}
} else {

28
framework/yii/widgets/ListView.php

@ -19,7 +19,7 @@ use yii\helpers\Html;
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".
* If "tag" is false, it means no container element will be rendered.
*/
@ -29,7 +29,7 @@ class ListView extends ListViewBase
* for rendering each data item. If it specifies a view name, the following variables will
* 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
* - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
* - `$widget`: ListView, this widget instance
@ -39,7 +39,7 @@ class ListView extends ListViewBase
* 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;
@ -50,40 +50,40 @@ class ListView extends ListViewBase
/**
* Renders all data items.
* Renders all data models.
* @return string the rendering result
*/
public function renderItems()
{
$items = $this->dataProvider->getItems();
$models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys();
$rows = array();
foreach (array_values($items) as $index => $item) {
$rows[] = $this->renderItem($item, $keys[$index], $index);
foreach (array_values($models) as $index => $model) {
$rows[] = $this->renderItem($model, $keys[$index], $index);
}
return implode($this->separator, $rows);
}
/**
* Renders a single data item.
* @param mixed $item the data item to be rendered
* @param mixed $key the key value associated with the data item
* @param integer $index the zero-based index of the data item in the item array returned by [[dataProvider]].
* Renders a single data model.
* @param mixed $model the data model to be rendered
* @param mixed $key the key value associated with the data model
* @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
* @return string the rendering result
*/
public function renderItem($item, $key, $index)
public function renderItem($model, $key, $index)
{
if ($this->itemView === null) {
$content = $key;
} elseif (is_string($this->itemView)) {
$content = $this->getView()->render($this->itemView, array(
'item' => $item,
'model' => $model,
'key' => $key,
'index' => $index,
'widget' => $this,
));
} else {
$content = call_user_func($this->itemView, $item, $key, $index, $this);
$content = call_user_func($this->itemView, $model, $key, $index, $this);
}
$options = $this->itemOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div');

2
framework/yii/widgets/ListViewBase.php

@ -70,7 +70,7 @@ abstract class ListViewBase extends Widget
/**
* Renders the data items.
* Renders the data models.
* @return string the rendering result.
*/
abstract public function renderItems();

16
framework/yii/widgets/grid/Column.php

@ -75,18 +75,18 @@ class Column extends Object
/**
* 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]].
* @return string the rendering result
*/
public function renderDataCell($item, $index)
public function renderDataCell($model, $index)
{
if ($this->bodyOptions instanceof Closure) {
$options = call_user_func($this->bodyOptions, $item, $index, $this);
$options = call_user_func($this->bodyOptions, $model, $index, $this);
} else {
$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
/**
* Renders the data cell content.
* @param mixed $item the data item
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]].
* @param mixed $model the data model
* @param integer $index the zero-based index of the data model among the models array returned by [[dataProvider]].
* @return string the rendering result
*/
protected function renderDataCellContent($item, $index)
protected function renderDataCellContent($model, $index)
{
if ($this->content !== null) {
return call_user_func($this->content, $item, $index, $this);
return call_user_func($this->content, $model, $index, $this);
} else {
return $this->grid->emptyCell;
}

17
framework/yii/widgets/grid/DataColumn.php

@ -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
* the list options.
* If you don't want a filter for this data column, set this value to false.
* @since 1.1.1
*/
public $filter;
@ -49,10 +48,10 @@ class DataColumn extends Column
if ($this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
return $sort->link($this->attribute);
}
$items = $provider->getItems();
if (($item = reset($items)) instanceof Model) {
/** @var Model $item */
return $item->getAttributeLabel($this->attribute);
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
/** @var Model $model */
return $model->getAttributeLabel($this->attribute);
} elseif ($provider instanceof ActiveDataProvider) {
if ($provider->query instanceof ActiveQuery) {
/** @var Model $model */
@ -81,14 +80,14 @@ class DataColumn extends Column
}
}
protected function renderDataCellContent($item, $index)
protected function renderDataCellContent($model, $index)
{
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) {
$value = ArrayHelper::getValue($item, $this->attribute);
$value = ArrayHelper::getValue($model, $this->attribute);
} else {
return parent::renderDataCellContent($item, $index);
return parent::renderDataCellContent($model, $index);
}
return $this->grid->formatter->format($value, $this->format);
}

8
tests/unit/framework/data/ActiveDataProviderTest.php

@ -30,7 +30,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
$provider = new ActiveDataProvider(array(
'query' => Order::find()->orderBy('id'),
));
$orders = $provider->getItems();
$orders = $provider->getModels();
$this->assertEquals(3, count($orders));
$this->assertTrue($orders[0] instanceof Order);
$this->assertEquals(array(1, 2, 3), $provider->getKeys());
@ -41,7 +41,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'pageSize' => 2,
)
));
$orders = $provider->getItems();
$orders = $provider->getModels();
$this->assertEquals(2, count($orders));
}
@ -52,7 +52,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'db' => $this->getConnection(),
'query' => $query->from('tbl_order')->orderBy('id'),
));
$orders = $provider->getItems();
$orders = $provider->getModels();
$this->assertEquals(3, count($orders));
$this->assertTrue(is_array($orders[0]));
$this->assertEquals(array(0, 1, 2), $provider->getKeys());
@ -65,7 +65,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'pageSize' => 2,
)
));
$orders = $provider->getItems();
$orders = $provider->getModels();
$this->assertEquals(2, count($orders));
}
}

Loading…
Cancel
Save