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.

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