Ejemplo n.º 1
0
 /**
  * Lists all Post models.
  * @return mixed
  */
 public function actionIndex()
 {
     $postSearchForm = new DynamicModel(['search']);
     $postSearchForm->addRule('search', 'string', ['length' => [3, 255], 'tooShort' => 'Поиск должен содержать минимум 3 символа.']);
     $query = Post::find()->distinct()->joinWith('categories')->where(['post.status' => 1])->orderBy('created DESC');
     $request = Yii::$app->request;
     $query->filterWhere(['category_id' => $request->getQueryParam('category')]);
     if ($postSearchForm->load($request->queryParams, '') && $postSearchForm->validate()) {
         $query->andFilterWhere(['OR', ['LIKE', 'title', $request->getQueryParam('search')], ['LIKE', 'anotation', $request->getQueryParam('search')], ['LIKE', 'text', $request->getQueryParam('search')]]);
     }
     $countQuery = clone $query;
     $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 3]);
     $models = $query->offset($pages->offset)->limit($pages->limit)->all();
     return $this->render('index', ['models' => $models, 'pages' => $pages, 'postSearchForm' => $postSearchForm]);
 }
Ejemplo n.º 2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Post::find()->orderBy('id DESC');
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'author_id' => $this->author_id, 'created' => $this->created, 'updated' => $this->updated, 'views' => $this->views, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'anotation', $this->anotation])->andFilterWhere(['like', 'text', $this->text]);
     return $dataProvider;
 }
Ejemplo n.º 3
0
 public function init()
 {
     parent::init();
     $posts = Post::find()->where(['status' => 1])->orderBy(['views' => SORT_DESC])->all();
     echo $this->render('posts', ['posts' => $posts]);
 }