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.

126 lines
3.2 KiB

  1. <?php
  2. namespace backend\models;
  3. use backend\models\Category;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. use yii\helpers\ArrayHelper;
  7. /**
  8. * CategorySearch represents the model behind the search form of `\backend\models\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', 'icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
  27. [['cat_name', 'icon', 'description', 'created_at_range'], 'safe'],
  28. ];
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function scenarios()
  34. {
  35. // bypass scenarios() implementation in the parent class
  36. return Model::scenarios();
  37. }
  38. /**
  39. * @param $params
  40. * @return ActiveDataProvider
  41. * 不分页的所有数据
  42. */
  43. public function allData($params)
  44. {
  45. $query = Category::find();
  46. $dataProvider = new ActiveDataProvider([
  47. 'query' => $query,
  48. 'pagination' => false,
  49. 'sort' => false
  50. ]);
  51. $this->load($params);
  52. return $this->filter($query, $dataProvider);
  53. }
  54. /**
  55. * Creates data provider instance with search query applied
  56. *
  57. * @param array $params
  58. *
  59. * @return ActiveDataProvider
  60. */
  61. public function search($params)
  62. {
  63. $query = Category::find();
  64. // add conditions that should always apply here
  65. $dataProvider = new ActiveDataProvider([
  66. 'query' => $query,
  67. 'pagination' => [
  68. 'pageSizeLimit' => [1, 200]
  69. ],
  70. 'sort' => [
  71. 'defaultOrder' => [
  72. 'id' => SORT_DESC,
  73. ]
  74. ],
  75. ]);
  76. $this->load($params);
  77. return $this->filter($query, $dataProvider);
  78. }
  79. /**
  80. * @param $query
  81. * @param $dataProvider
  82. * @return ActiveDataProvider
  83. * 条件筛选
  84. */
  85. private function filter($query, $dataProvider)
  86. {
  87. if (!$this->validate()) {
  88. // uncomment the following line if you do not want to return any records when validation fails
  89. // $query->where('0=1');
  90. return $dataProvider;
  91. }
  92. // grid filtering conditions
  93. $query->andFilterWhere([
  94. 'id' => $this->id,
  95. 'icon_type' => $this->icon_type,
  96. 'sort_order' => $this->sort_order,
  97. 'created_at' => $this->created_at,
  98. 'updated_at' => $this->updated_at,
  99. ]);
  100. $query->andFilterWhere(['like', 'cat_name', $this->cat_name])
  101. ->andFilterWhere(['like', 'icon', $this->icon])
  102. ->andFilterWhere(['like', 'description', $this->description]);
  103. if ($this->created_at_range) {
  104. $arr = explode(' ~ ', $this->created_at_range);
  105. $start = strtotime($arr[0]);
  106. $end = strtotime($arr[1]) + 3600 * 24;
  107. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  108. }
  109. return $dataProvider;
  110. }
  111. }