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.

161 lines
4.5 KiB

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