ArrayDataProvider.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\data;
  8. use yii\helpers\ArrayHelper;
  9. /**
  10. * ArrayDataProvider implements a data provider based on a data array.
  11. *
  12. * The [[allModels]] property contains all data models that may be sorted and/or paginated.
  13. * ArrayDataProvider will provide the data after sorting and/or pagination.
  14. * You may configure the [[sort]] and [[pagination]] properties to
  15. * customize the sorting and pagination behaviors.
  16. *
  17. * Elements in the [[allModels]] array may be either objects (e.g. model objects)
  18. * or associative arrays (e.g. query results of DAO).
  19. * Make sure to set the [[key]] property to the name of the field that uniquely
  20. * identifies a data record or false if you do not have such a field.
  21. *
  22. * Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient
  23. * because it needs to have [[allModels]] ready.
  24. *
  25. * ArrayDataProvider may be used in the following way:
  26. *
  27. * ```php
  28. * $query = new Query;
  29. * $provider = new ArrayDataProvider([
  30. * 'allModels' => $query->from('post')->all(),
  31. * 'sort' => [
  32. * 'attributes' => ['id', 'username', 'email'],
  33. * ],
  34. * 'pagination' => [
  35. * 'pageSize' => 10,
  36. * ],
  37. * ]);
  38. * // get the posts in the current page
  39. * $posts = $provider->getModels();
  40. * ```
  41. *
  42. * Note: if you want to use the sorting feature, you must configure the [[sort]] property
  43. * so that the provider knows which columns can be sorted.
  44. *
  45. * @author Qiang Xue <qiang.xue@gmail.com>
  46. * @since 2.0
  47. */
  48. class ArrayDataProvider extends BaseDataProvider
  49. {
  50. /**
  51. * @var string|callable the column that is used as the key of the data models.
  52. * This can be either a column name, or a callable that returns the key value of a given data model.
  53. * If this is not set, the index of the [[models]] array will be used.
  54. * @see getKeys()
  55. */
  56. public $key;
  57. /**
  58. * @var array the data that is not paginated or sorted. When pagination is enabled,
  59. * this property usually contains more elements than [[models]].
  60. * The array elements must use zero-based integer keys.
  61. */
  62. public $allModels;
  63. /**
  64. * @var string the name of the [[\yii\base\Model|Model]] class that will be represented.
  65. * This property is used to get columns' names.
  66. * @since 2.0.9
  67. */
  68. public $modelClass;
  69. /**
  70. * @inheritdoc
  71. */
  72. protected function prepareModels()
  73. {
  74. if (($models = $this->allModels) === null) {
  75. return [];
  76. }
  77. if (($sort = $this->getSort()) !== false) {
  78. $models = $this->sortModels($models, $sort);
  79. }
  80. if (($pagination = $this->getPagination()) !== false) {
  81. $pagination->totalCount = $this->getTotalCount();
  82. if ($pagination->getPageSize() > 0) {
  83. $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
  84. }
  85. }
  86. return $models;
  87. }
  88. /**
  89. * @inheritdoc
  90. */
  91. protected function prepareKeys($models)
  92. {
  93. if ($this->key !== null) {
  94. $keys = [];
  95. foreach ($models as $model) {
  96. if (is_string($this->key)) {
  97. $keys[] = $model[$this->key];
  98. } else {
  99. $keys[] = call_user_func($this->key, $model);
  100. }
  101. }
  102. return $keys;
  103. } else {
  104. return array_keys($models);
  105. }
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. protected function prepareTotalCount()
  111. {
  112. return count($this->allModels);
  113. }
  114. /**
  115. * Sorts the data models according to the given sort definition
  116. * @param array $models the models to be sorted
  117. * @param Sort $sort the sort definition
  118. * @return array the sorted data models
  119. */
  120. protected function sortModels($models, $sort)
  121. {
  122. $orders = $sort->getOrders();
  123. if (!empty($orders)) {
  124. ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
  125. }
  126. return $models;
  127. }
  128. }