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.

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