You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.1 KiB

  1. <?php
  2. namespace common\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use common\models\ars\Category;
  7. /**
  8. * CategorySearch represents the model behind the search form of `common\models\ars\Category`.
  9. */
  10. class CategorySearch extends Category
  11. {
  12. /**
  13. * @return array
  14. * 增加创建时间查询字段
  15. */
  16. public function attributes()
  17. {
  18. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [['id', 'pid', 'goods_count', 'sort_order', 'icon_type', 'is_show', 'is_delete', 'created_at', 'updated_at'], 'integer'],
  27. [['name', 'icon'], 'safe'],
  28. ['created_at_range','safe'],
  29. ];
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function scenarios()
  35. {
  36. // bypass scenarios() implementation in the parent class
  37. return Model::scenarios();
  38. }
  39. /**
  40. * @return array
  41. * 列格式
  42. */
  43. public function columns()
  44. {
  45. return [
  46. [
  47. 'class' => 'blobt\grid\CheckboxColumn',
  48. 'width' => '2%',
  49. 'align' => 'center'
  50. ],
  51. 'id',
  52. 'name',
  53. 'pid',
  54. 'goods_count',
  55. 'sort_order',
  56. //'icon_type',
  57. //'icon',
  58. //'is_show',
  59. //'is_delete',
  60. //'created_at',
  61. //'updated_at',
  62. [
  63. 'class' => 'iron\grid\ActionColumn',
  64. 'align' => 'center',
  65. ],
  66. ];
  67. }
  68. /**
  69. * @param $params
  70. * @return ActiveDataProvider
  71. * 不分页的所有数据
  72. */
  73. public function allData($params)
  74. {
  75. $query = Category::find();
  76. $dataProvider = new ActiveDataProvider([
  77. 'query' => $query,
  78. 'pagination' => false,
  79. 'sort' => false
  80. ]);
  81. $this->load($params);
  82. return $this->filter($query, $dataProvider);
  83. }
  84. /**
  85. * Creates data provider instance with search query applied
  86. *
  87. * @param array $params
  88. *
  89. * @return ActiveDataProvider
  90. */
  91. public function search($params)
  92. {
  93. $query = Category::find();
  94. // add conditions that should always apply here
  95. $dataProvider = new ActiveDataProvider([
  96. 'query' => $query,
  97. 'pagination' => [
  98. 'pageSizeLimit' => [1, 200]
  99. ],
  100. 'sort' => [
  101. 'defaultOrder' => [
  102. 'id' => SORT_DESC,
  103. ]
  104. ],
  105. ]);
  106. $this->load($params);
  107. return $this->filter($query, $dataProvider);
  108. }
  109. /**
  110. * @param $query
  111. * @param $dataProvider
  112. * @return ActiveDataProvider
  113. * 条件筛选
  114. */
  115. private function filter($query, $dataProvider){
  116. if (!$this->validate()) {
  117. // uncomment the following line if you do not want to return any records when validation fails
  118. // $query->where('0=1');
  119. return $dataProvider;
  120. }
  121. // grid filtering conditions
  122. $query->andFilterWhere([
  123. 'id' => $this->id,
  124. 'pid' => $this->pid,
  125. 'goods_count' => $this->goods_count,
  126. 'sort_order' => $this->sort_order,
  127. 'icon_type' => $this->icon_type,
  128. 'is_show' => $this->is_show,
  129. 'is_delete' => $this->is_delete,
  130. 'created_at' => $this->created_at,
  131. 'updated_at' => $this->updated_at,
  132. ]);
  133. $query->andFilterWhere(['like', 'name', $this->name])
  134. ->andFilterWhere(['like', 'icon', $this->icon]);
  135. if ($this->created_at_range) {
  136. $arr = explode(' ~ ', $this->created_at_range);
  137. $start = strtotime($arr[0]);
  138. $end = strtotime($arr[1]) + 3600 * 24;
  139. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  140. }
  141. return $dataProvider;
  142. }
  143. }