/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find()->joinWith('translations'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * Displays homepage. * * @return mixed */ public function actionIndex($slug = 'index') { if (empty($slug) || $slug == 'index') { throw new NotFoundHttpException('Page not found.'); } else { $tag = Tag::find()->where(['slug' => $slug]); $tagCount = clone $tag; if (!$tagCount->count()) { throw new NotFoundHttpException('Page not found.'); } } $query = Post::find()->joinWith('tags')->where(['status' => Post::STATUS_PUBLISHED, Tag::tableName() . '.slug' => $slug])->orderBy('published_at DESC'); $countQuery = clone $query; $pagination = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => Yii::$app->settings->get('reading.page_size', 10)]); $posts = $query->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('index', ['posts' => $posts, 'tag' => $tag->one(), 'pagination' => $pagination]); }