Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array   $params
  * @param integer $pageSize  The number of results to be displayed per page.
  * @param boolean $published Whether or not articles have to be published.
  *
  * @return ActiveDataProvider
  */
 public function search($params, $pageSize = 10, $published = false)
 {
     $query = Article::find();
     // this means that editor is trying to see articles
     // we will allow him to see published ones and drafts made by him
     if ($published === true) {
         $query->where(['status' => Article::STATUS_PUBLISHED])->orWhere(['user_id' => Yii::$app->user->id]);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => $pageSize]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'status' => $this->status, 'category' => $this->category]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'summary', $this->summary])->andFilterWhere(['like', 'content', $this->content]);
     return $dataProvider;
 }
Example #2
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 (($model = Article::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }