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.

147 lines
3.9 KiB

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