SurveySearch.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace backend\models\search;
  3. use common\models\Survey;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. /**
  7. * SurveySearch represents the model behind the search form about `common\models\Survey`.
  8. */
  9. class SurveySearch extends Survey
  10. {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function rules()
  15. {
  16. return [
  17. [['id', 'category_id', 'created_at', 'updated_at', 'status'], 'integer'],
  18. [['title', 'category', 'cover', 'module'], 'string'],
  19. ];
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function scenarios()
  25. {
  26. // bypass scenarios() implementation in the parent class
  27. return Model::scenarios();
  28. }
  29. /**
  30. * Creates data provider instance with search query applied.
  31. *
  32. * @param array $params
  33. *
  34. * @return ActiveDataProvider
  35. */
  36. public function search($params)
  37. {
  38. $query = Survey::find();
  39. $dataProvider = new ActiveDataProvider([
  40. 'query' => $query,
  41. 'sort' => [
  42. 'defaultOrder' => [
  43. 'id' => SORT_ASC
  44. ]
  45. ]
  46. ]);
  47. $this->load($params);
  48. if (!$this->validate()) {
  49. // uncomment the following line if you do not want to return any records when validation fails
  50. // $query->where('0=1');
  51. return $dataProvider;
  52. }
  53. $query->andFilterWhere([
  54. 'id' => $this->id,
  55. 'status' => $this->status,
  56. 'module' => $this->module,
  57. ]);
  58. $query->andFilterWhere(['like', 'title', $this->title]);
  59. return $dataProvider;
  60. }
  61. }