CommentSearch.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace backend\models\search;
  3. use common\models\Comment;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. /**
  7. * Comment represents the model behind the search form about `common\models\Article`.
  8. */
  9. class CommentSearch extends Comment
  10. {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function rules()
  15. {
  16. return [
  17. [['id', 'entity_id', 'user_id'], 'integer'],
  18. [['entity'], '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 = Comment::find();
  39. $dataProvider = new ActiveDataProvider([
  40. 'query' => $query,
  41. 'sort' => [
  42. 'defaultOrder' => [
  43. 'id' => SORT_DESC
  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. 'entity' => $this->entity,
  56. 'entity_id' => $this->entity_id,
  57. 'user_id' => $this->user_id,
  58. ]);
  59. return $dataProvider;
  60. }
  61. }