ArrayDataProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * ~~~
  28. * $query = new Query;
  29. * $provider = new ArrayDataProvider([
  30. * 'allModels' => $query->from('tbl_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 <[email protected]>
  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. * @inheritdoc
  65. */
  66. protected function prepareModels()
  67. {
  68. if (($models = $this->allModels) === null) {
  69. return [];
  70. }
  71. if (($sort = $this->getSort()) !== false) {
  72. $models = $this->sortModels($models, $sort);
  73. }
  74. if (($pagination = $this->getPagination()) !== false) {
  75. $pagination->totalCount = $this->getTotalCount();
  76. $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit());
  77. }
  78. return $models;
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. protected function prepareKeys($models)
  84. {
  85. if ($this->key !== null) {
  86. $keys = [];
  87. foreach ($models as $model) {
  88. if (is_string($this->key)) {
  89. $keys[] = $model[$this->key];
  90. } else {
  91. $keys[] = call_user_func($this->key, $model);
  92. }
  93. }
  94. return $keys;
  95. } else {
  96. return array_keys($models);
  97. }
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. protected function prepareTotalCount()
  103. {
  104. return count($this->allModels);
  105. }
  106. /**
  107. * Sorts the data models according to the given sort definition
  108. * @param array $models the models to be sorted
  109. * @param Sort $sort the sort definition
  110. * @return array the sorted data models
  111. */
  112. protected function sortModels($models, $sort)
  113. {
  114. $orders = $sort->getOrders();
  115. if (!empty($orders)) {
  116. ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
  117. }
  118. return $models;
  119. }
  120. }