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.

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