BlogPostsSearch.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\models;
  3. use app\models\Categoriesblog;
  4. use Yii;
  5. use yii\base\Model;
  6. use yii\data\ActiveDataProvider;
  7. use app\models\BlogPosts;
  8. /**
  9. * BlogPostsSearch represents the model behind the search form about `app\models\BlogPosts`.
  10. */
  11. class BlogPostsSearch extends BlogPosts
  12. {
  13. public $categorySlug;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function rules()
  18. {
  19. return [
  20. [['id', 'cat_id'], 'integer'],
  21. [['title', 'text', 'picture','categorySlug','description','key_words',], 'safe'],
  22. ];
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function scenarios()
  28. {
  29. // bypass scenarios() implementation in the parent class
  30. return Model::scenarios();
  31. }
  32. /**
  33. * Creates data provider instance with search query applied
  34. *
  35. * @param array $params
  36. *
  37. * @return ActiveDataProvider
  38. */
  39. public function search($params, $formName = 'BlogPostsSearch')
  40. {
  41. $query = BlogPosts::find();
  42. // add conditions that should always apply here
  43. $dataProvider = new ActiveDataProvider([
  44. 'query' => $query,
  45. 'sort' => [
  46. 'defaultOrder'=>[
  47. 'id'=> SORT_DESC
  48. ],
  49. ],
  50. 'pagination'=>[
  51. 'pageSize' => 6,
  52. ],
  53. ]);
  54. $this->load($params,$formName);
  55. if (!$this->validate()) {
  56. // uncomment the following line if you do not want to return any records when validation fails
  57. // $query->where('0=1');
  58. return $dataProvider;
  59. }
  60. $query->joinWith('category');
  61. // grid filtering conditions
  62. $query->andFilterWhere([
  63. 'id' => $this->id,
  64. 'cat_id' => $this->cat_id,
  65. ]);
  66. $query->andFilterWhere(['like', 'title', $this->title])
  67. ->andFilterWhere(['like', 'text', $this->text])
  68. ->andFilterWhere(['like', 'picture', $this->picture])
  69. ->andFilterWhere(['like', 'posts.description', $this->description])
  70. ->andFilterWhere(['like', 'key_words', $this->key_words])
  71. ->andFilterWhere(['like', 'categoriesblog.slug', $this->categorySlug]);
  72. return $dataProvider;
  73. }
  74. }