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.

152 lines
4.0 KiB

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