public function actionIndex()
 {
     $categoryId = \Yii::$app->request->get('id');
     $list = [Variable::$articleCat_type_good, Variable::$articleCat_type_nutrition, Variable::$articleCat_type_people];
     if (!in_array($categoryId, $list)) {
         $categoryId = Variable::$articleCat_type_nutrition;
     }
     $model = Article::findOne(['categoryId' => $categoryId]);
     $modelList = Article::find()->where(['in', 'categoryId', $list])->all();
     return $this->render(FVariable::$lifeIndex_view, ['model' => $model, 'modelList' => $modelList, 'id' => $categoryId]);
 }
 public function actionDetail()
 {
     $id = Yii::$app->request->get('id');
     if (empty($id)) {
         $this->redirect(FVariable::$articleIndex_url);
         return;
     }
     (new Article())->updateArticleLookCount($id);
     $model = Article::findOne($id);
     $formerModel = Article::find()->where(['<', 'id', $id])->andWhere(['categoryId' => Variable::$articleCat_type_news])->orderBy('id DESC')->limit(1)->one();
     $laterModel = Article::find()->where(['>', 'id', $id])->andWhere(['categoryId' => Variable::$articleCat_type_news])->orderBy('id DESC')->limit(1)->one();
     return $this->render(FVariable::$articleDetail_view, ['model' => $model, 'formerModel' => $formerModel, 'laterModel' => $laterModel]);
 }
Beispiel #3
0
 public function updateArticleLookCount($id)
 {
     $model = Article::findOne($id);
     if (!$model) {
         return false;
     }
     $cur = intval($model->lookCount);
     if ($cur < 1) {
         $cur = 0;
     }
     $model->lookCount = $cur + 1;
     if ($model->save()) {
         return true;
     }
     return false;
 }
 /**
  * Finds the Article model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * 
  * @param integer  $id
  * @return Article The loaded model.
  * 
  * @throws NotFoundHttpException if the model cannot be found.
  */
 protected function findModel($id)
 {
     if (($model = Article::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }