/** * Возвращает модель категории. * @param int $id идентификатор категории * @throws NotFoundHttpException в случае, когда категория не найдена * @return Post */ public function getCategory($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested post does not exist.'); } }
public function actionBycategory($category_id) { $cat = new Category(); $model = $cat->findOne($category_id); $category_title = $model->name; $dataProvider = new ActiveDataProvider(['query' => Post::find()->where(['status' => 1, 'category_id' => $category_id])->with('category')->orderBy('created_at DESC'), 'pagination' => ['pageSize' => 10]]); return $this->render('categoryindex', ['listDataProvider' => $dataProvider, 'category_title' => $category_title]); }
/** * Finds the Category model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Category the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }