示例#1
0
 /**
  * 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.');
     }
 }