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.

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