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.

113 lines
2.9 KiB

  1. <?php
  2. namespace common\models;
  3. use common\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 `\common\models\Category`.
  9. */
  10. class CategorySearch extends Category
  11. {
  12. public function attributes()
  13. {
  14. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function rules()
  20. {
  21. return [
  22. [['id', 'icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
  23. [['cat_name', 'icon', 'description', 'created_at_range'], 'safe'],
  24. ];
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function scenarios()
  30. {
  31. // bypass scenarios() implementation in the parent class
  32. return Model::scenarios();
  33. }
  34. public function allData($params)
  35. {
  36. $query = Category::find();
  37. $dataProvider = new ActiveDataProvider([
  38. 'query' => $query,
  39. 'pagination' => false,
  40. 'sort' => false
  41. ]);
  42. $this->load($params);
  43. return $this->filter($query, $dataProvider);
  44. }
  45. /**
  46. * Creates data provider instance with search query applied
  47. *
  48. * @param array $params
  49. *
  50. * @return ActiveDataProvider
  51. */
  52. public function search($params)
  53. {
  54. $query = Category::find();
  55. // add conditions that should always apply here
  56. $dataProvider = new ActiveDataProvider([
  57. 'query' => $query,
  58. 'pagination' => [
  59. 'pageSizeLimit' => [1, 200]
  60. ],
  61. 'sort' => [
  62. 'defaultOrder' => [
  63. 'id' => SORT_DESC,
  64. ]
  65. ],
  66. ]);
  67. $this->load($params);
  68. return $this->filter($query, $dataProvider);
  69. }
  70. private function filter($query, $dataProvider)
  71. {
  72. if (!$this->validate()) {
  73. // uncomment the following line if you do not want to return any records when validation fails
  74. // $query->where('0=1');
  75. return $dataProvider;
  76. }
  77. // grid filtering conditions
  78. $query->andFilterWhere([
  79. 'id' => $this->id,
  80. 'icon_type' => $this->icon_type,
  81. 'sort_order' => $this->sort_order,
  82. 'created_at' => $this->created_at,
  83. 'updated_at' => $this->updated_at,
  84. ]);
  85. $query->andFilterWhere(['like', 'cat_name', $this->cat_name])
  86. ->andFilterWhere(['like', 'icon', $this->icon])
  87. ->andFilterWhere(['like', 'description', $this->description]);
  88. if ($this->created_at_range) {
  89. $arr = explode(' ~ ', $this->created_at_range);
  90. $start = strtotime($arr[0]);
  91. $end = strtotime($arr[1]) + 3600 * 24;
  92. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  93. }
  94. return $dataProvider;
  95. }
  96. }