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.

159 lines
4.4 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\ShopCategory;
  7. /**
  8. * ShopCategorySearch represents the model behind the search form of `common\models\ars\ShopCategory`.
  9. */
  10. class ShopCategorySearch extends ShopCategory
  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', 'keywords', 'desc', 'icon', 'filter_attr'], '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. 'keywords',
  56. //'desc',
  57. //'sort_order',
  58. //'icon_type',
  59. //'icon',
  60. //'filter_attr',
  61. //'is_show',
  62. //'is_delete',
  63. //'created_at',
  64. //'updated_at',
  65. [
  66. 'class' => 'iron\grid\ActionColumn',
  67. 'align' => 'center',
  68. ],
  69. ];
  70. }
  71. /**
  72. * @param $params
  73. * @return ActiveDataProvider
  74. * 不分页的所有数据
  75. */
  76. public function allData($params)
  77. {
  78. $query = ShopCategory::find();
  79. $dataProvider = new ActiveDataProvider([
  80. 'query' => $query,
  81. 'pagination' => false,
  82. 'sort' => false
  83. ]);
  84. $this->load($params);
  85. return $this->filter($query, $dataProvider);
  86. }
  87. /**
  88. * Creates data provider instance with search query applied
  89. *
  90. * @param array $params
  91. *
  92. * @return ActiveDataProvider
  93. */
  94. public function search($params)
  95. {
  96. $query = ShopCategory::find();
  97. // add conditions that should always apply here
  98. $dataProvider = new ActiveDataProvider([
  99. 'query' => $query,
  100. 'pagination' => [
  101. 'pageSizeLimit' => [1, 200]
  102. ],
  103. 'sort' => [
  104. 'defaultOrder' => [
  105. 'id' => SORT_DESC,
  106. ]
  107. ],
  108. ]);
  109. $this->load($params);
  110. return $this->filter($query, $dataProvider);
  111. }
  112. /**
  113. * @param $query
  114. * @param $dataProvider
  115. * @return ActiveDataProvider
  116. * 条件筛选
  117. */
  118. private function filter($query, $dataProvider){
  119. if (!$this->validate()) {
  120. // uncomment the following line if you do not want to return any records when validation fails
  121. // $query->where('0=1');
  122. return $dataProvider;
  123. }
  124. // grid filtering conditions
  125. $query->andFilterWhere([
  126. 'id' => $this->id,
  127. 'pid' => $this->pid,
  128. 'goods_count' => $this->goods_count,
  129. 'sort_order' => $this->sort_order,
  130. 'icon_type' => $this->icon_type,
  131. 'is_show' => $this->is_show,
  132. 'is_delete' => $this->is_delete,
  133. 'created_at' => $this->created_at,
  134. 'updated_at' => $this->updated_at,
  135. ]);
  136. $query->andFilterWhere(['like', 'name', $this->name])
  137. ->andFilterWhere(['like', 'keywords', $this->keywords])
  138. ->andFilterWhere(['like', 'desc', $this->desc])
  139. ->andFilterWhere(['like', 'icon', $this->icon])
  140. ->andFilterWhere(['like', 'filter_attr', $this->filter_attr]);
  141. if ($this->created_at_range) {
  142. $arr = explode(' ~ ', $this->created_at_range);
  143. $start = strtotime($arr[0]);
  144. $end = strtotime($arr[1]) + 3600 * 24;
  145. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  146. }
  147. return $dataProvider;
  148. }
  149. }