예제 #1
0
파일: NewsController.php 프로젝트: apuc/api
 /**
  * Creates a new News model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new News();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $model->dt_add = time();
         $model->status = 1;
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
예제 #2
0
파일: LastNews.php 프로젝트: apuc/api
 public function run()
 {
     $news = News::getDb()->cache(function () {
         return News::find()->orderBy('dt_add DESC')->limit(4)->all();
     });
     return $this->render('last_news', ['news' => $news]);
 }
예제 #3
0
파일: NewsSearch.php 프로젝트: apuc/NDFL
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = News::find();
     $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;
     }
     $query->andFilterWhere(['id' => $this->id, 'dt_add' => $this->dt_add, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'tags', $this->tags])->andFilterWhere(['like', 'text', $this->text]);
     return $dataProvider;
 }
예제 #4
0
 /**
  * Finds the News model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return News the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = News::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }