コード例 #1
0
 /**
  * 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 ($id !== null && ($model = Article::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('请求页面不存在');
     }
 }
コード例 #2
0
 public function actionView($slug)
 {
     $model = new Comment();
     if ($model->load(Yii::$app->request->post())) {
         $model->created_at = new Carbon();
         $model->updated_at = new Carbon();
         $model->user_id = Yii::$app->user->identity->id;
         if ($model->save()) {
             $model->article->updateAllCounters(['comments_count' => 1]);
             return $this->redirect(['view', 'slug' => $slug]);
         }
     }
     $article = Article::findOne(['slug' => $slug]);
     if (is_null($article)) {
         throw new NotFoundHttpException('请求页面不存在');
     }
     return $this->render('view', ['article' => $article, 'model' => $model]);
 }
コード例 #3
0
ファイル: ArticleSearch.php プロジェクト: NikDevPHP/yii2-blog
 public function search($params)
 {
     $query = Article::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $this->addCondition($query, 'id');
     $this->addCondition($query, 'category_id');
     $this->addCondition($query, 'user_id');
     $this->addCondition($query, 'title', true);
     $this->addCondition($query, 'slug', true);
     $this->addCondition($query, 'content', true);
     $this->addCondition($query, 'content_format', true);
     $this->addCondition($query, 'comments_count');
     $this->addCondition($query, 'meta_title', true);
     $this->addCondition($query, 'meta_description', true);
     $this->addCondition($query, 'meta_keywords', true);
     $this->addCondition($query, 'created_at');
     $this->addCondition($query, 'updated_at');
     $this->addCondition($query, 'deleted_at');
     return $dataProvider;
 }