Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ArrayDataProvider.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * @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. if ($pagination->getPageSize() > 0) {
  77. $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit());
  78. }
  79. }
  80. return $models;
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. protected function prepareKeys($models)
  86. {
  87. if ($this->key !== null) {
  88. $keys = [];
  89. foreach ($models as $model) {
  90. if (is_string($this->key)) {
  91. $keys[] = $model[$this->key];
  92. } else {
  93. $keys[] = call_user_func($this->key, $model);
  94. }
  95. }
  96. return $keys;
  97. } else {
  98. return array_keys($models);
  99. }
  100. }
  101. /**
  102. * @inheritdoc
  103. */
  104. protected function prepareTotalCount()
  105. {
  106. return count($this->allModels);
  107. }
  108. /**
  109. * Sorts the data models according to the given sort definition
  110. * @param array $models the models to be sorted
  111. * @param Sort $sort the sort definition
  112. * @return array the sorted data models
  113. */
  114. protected function sortModels($models, $sort)
  115. {
  116. $orders = $sort->getOrders();
  117. if (!empty($orders)) {
  118. ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
  119. }
  120. return $models;
  121. }
  122. }