CommentSearch.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-06-11 22:11
  7. */
  8. namespace backend\models\search;
  9. use Yii;
  10. use common\models\Comment;
  11. use backend\behaviors\TimeSearchBehavior;
  12. use backend\components\search\SearchEvent;
  13. use common\models\Article;
  14. use yii\data\ActiveDataProvider;
  15. class CommentSearch extends Comment implements SearchInterface
  16. {
  17. public $article_title;
  18. public function behaviors()
  19. {
  20. return [
  21. TimeSearchBehavior::className()
  22. ];
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['article_title', 'created_at', 'updated_at', 'nickname', 'content'], 'string'],
  31. [['aid', 'status'], 'integer'],
  32. ];
  33. }
  34. /**
  35. * @param array $params
  36. * @param array $options
  37. * @return ActiveDataProvider
  38. * @throws \yii\base\InvalidConfigException
  39. */
  40. public function search(array $params = [], array $options = [])
  41. {
  42. $query = Comment::find()->with('article');
  43. /** @var ActiveDataProvider $dataProvider */
  44. $dataProvider = Yii::createObject([
  45. 'class' => ActiveDataProvider::className(),
  46. 'query' => $query,
  47. 'sort' => [
  48. 'defaultOrder' => [
  49. 'id' => SORT_DESC,
  50. ]
  51. ]
  52. ]);
  53. $this->load($params);
  54. if (! $this->validate()) {
  55. return $dataProvider;
  56. }
  57. $query->andFilterWhere(['like', 'nickname', $this->nickname])
  58. ->andFilterWhere(['status' => $this->status])
  59. ->andFilterWhere(['aid' => $this->aid])
  60. ->andFilterWhere(['like', 'content', $this->content]);
  61. if ($this->article_title != '') {
  62. $articles = Article::find()
  63. ->where(['like', 'title', $this->article_title])
  64. ->select(['id', 'title'])
  65. ->indexBy('id')
  66. ->asArray()
  67. ->all();
  68. $aidArray = [];
  69. foreach ($articles as $k => $v) {
  70. array_push($aidArray, $k);
  71. }
  72. $query->andFilterWhere(['aid' => $aidArray]);
  73. }
  74. $this->trigger(SearchEvent::BEFORE_SEARCH, Yii::createObject(['class' => SearchEvent::className(), 'query'=>$query]));
  75. return $dataProvider;
  76. }
  77. }