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.

174 lines
4.8 KiB

  1. <?php
  2. namespace backend\modules\shop\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use backend\modules\shop\models\ars\TakingSite;
  7. use backend\modules\shop\models\ars\Province;
  8. use backend\modules\shop\models\ars\City;
  9. use backend\modules\shop\models\ars\Area;
  10. /**
  11. * TakingSiteSearch represents the model behind the search form of ` backend\modules\shop\models\ars\TakingSite`.
  12. */
  13. class TakingSiteSearch extends TakingSite
  14. {
  15. /**
  16. * @return array
  17. * 增加创建时间查询字段
  18. */
  19. public function attributes()
  20. {
  21. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['id', 'updated_at', 'created_at'], 'integer'],
  30. [['name', 'province', 'city', 'area', 'address'], 'safe'],
  31. ['created_at_range','safe'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function scenarios()
  38. {
  39. // bypass scenarios() implementation in the parent class
  40. return Model::scenarios();
  41. }
  42. /**
  43. * @return array
  44. * 列格式
  45. */
  46. public function columns()
  47. {
  48. return [
  49. [
  50. 'class' => 'blobt\grid\CheckboxColumn',
  51. 'width' => '2%',
  52. 'align' => 'center'
  53. ],
  54. 'id',
  55. 'name',
  56. [
  57. 'attribute' => 'province',
  58. 'value' => function ($model) {
  59. $province = Province::findOne(['province_id' => $model->province]);
  60. if ($province) {
  61. return $province->name;
  62. }
  63. }
  64. ],
  65. [
  66. 'attribute' => 'city',
  67. 'value' => function ($model) {
  68. $city = City::findOne(['city_id' => $model->city]);
  69. if ($city) {
  70. return $city->name;
  71. }
  72. }
  73. ],
  74. [
  75. 'attribute' => 'area',
  76. 'value' => function ($model) {
  77. $area = Area::findOne(['area_id' => $model->area]);
  78. if ($area) {
  79. return $area->name;
  80. }
  81. }
  82. ],
  83. //'address',
  84. //'updated_at',
  85. //'created_at',
  86. [
  87. 'class' => 'iron\grid\ActionColumn',
  88. 'align' => 'center',
  89. ],
  90. ];
  91. }
  92. /**
  93. * @param $params
  94. * @return ActiveDataProvider
  95. * 不分页的所有数据
  96. */
  97. public function allData($params)
  98. {
  99. $query = TakingSite::find();
  100. $dataProvider = new ActiveDataProvider([
  101. 'query' => $query,
  102. 'pagination' => false,
  103. 'sort' => false
  104. ]);
  105. $this->load($params);
  106. return $this->filter($query, $dataProvider);
  107. }
  108. /**
  109. * Creates data provider instance with search query applied
  110. *
  111. * @param array $params
  112. *
  113. * @return ActiveDataProvider
  114. */
  115. public function search($params)
  116. {
  117. $query = TakingSite::find();
  118. // add conditions that should always apply here
  119. $dataProvider = new ActiveDataProvider([
  120. 'query' => $query,
  121. 'pagination' => [
  122. 'pageSizeLimit' => [1, 200]
  123. ],
  124. 'sort' => [
  125. 'defaultOrder' => [
  126. 'id' => SORT_DESC,
  127. ]
  128. ],
  129. ]);
  130. $this->load($params);
  131. return $this->filter($query, $dataProvider);
  132. }
  133. /**
  134. * @param $query
  135. * @param $dataProvider
  136. * @return ActiveDataProvider
  137. * 条件筛选
  138. */
  139. private function filter($query, $dataProvider){
  140. if (!$this->validate()) {
  141. // uncomment the following line if you do not want to return any records when validation fails
  142. // $query->where('0=1');
  143. return $dataProvider;
  144. }
  145. // grid filtering conditions
  146. $query->andFilterWhere([
  147. 'id' => $this->id,
  148. 'updated_at' => $this->updated_at,
  149. 'created_at' => $this->created_at,
  150. ]);
  151. $query->andFilterWhere(['like', 'name', $this->name])
  152. ->andFilterWhere(['like', 'province', $this->province])
  153. ->andFilterWhere(['like', 'city', $this->city])
  154. ->andFilterWhere(['like', 'area', $this->area])
  155. ->andFilterWhere(['like', 'address', $this->address]);
  156. if ($this->created_at_range) {
  157. $arr = explode(' ~ ', $this->created_at_range);
  158. $start = strtotime($arr[0]);
  159. $end = strtotime($arr[1]) + 3600 * 24;
  160. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  161. }
  162. return $dataProvider;
  163. }
  164. }