/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Article::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, 'create_at' => $this->create_at, 'update_at' => $this->update_at, 'times' => $this->times, 'status' => $this->status, 'type_id' => $this->type_id]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'create_by', $this->create_by])->andFilterWhere(['like', 'update_by', $this->update_by]); $query->addOrderBy(['id' => SORT_DESC]); return $dataProvider; }
public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Article::find([])->orderBy('create_at desc')->limit(20), 'pagination' => ['pageSize' => 20]]); $hot_dataProvider = new ActiveDataProvider(['query' => Article::find([])->orderBy('times desc')->limit(20), 'pagination' => ['pageSize' => 20]]); // $dataProvider = new ArrayDataProvider([ // 'allModels'=>[ // ['title'=>'LAMP环境搭建'], // ['title'=>'大数据时代'], // ['title'=>'如果那么,那么~~~~~~~~~'], // ['title'=>'LAMP环境搭建'], // ['title'=>'大数据时代'], // ['title'=>'如果那么,那么~~~~~~~~~'], // ] // ]); return $this->render('index', ['dataProvider' => $dataProvider, 'hot_dataProvider' => $hot_dataProvider]); }
/** * @return \yii\db\ActiveQuery */ public function getArticles() { return $this->hasMany(Article::className(), ['type_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getArticle() { return $this->hasOne(Article::className(), ['id' => 'article_id']); }
public function actionView($id) { $model = Article::findOne($id); Article::updateAllCounters(['times' => 1], ['id' => $id]); return $this->render('view', ['model' => $model]); }
/** * 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) { $data = $model->getTags()->all(); if ($data) { $tags = []; foreach ($data as $row) { $tags[] = $row->tag; } $model->tags = implode(',', $tags); } return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }