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.

91 lines
2.5 KiB

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