public function init() { parent::init(); $this->error_code = 0; // Проверка наличия в базе категории с заданим id $this->categories = Categories::find()->where(['id' => $this->cat_id])->one(); if (!$this->categories) { if (!$this->cat_id) { //Виборка всех статей сортю по дате в одратном порядке $this->articles = Articles::find()->orderBy(['date' => SORT_DESC])->all(); //.. проверка успешности операции if (!$this->articles) { $this->error_code = 2; } } else { $this->error_code = 1; } } else { //Запись названия категории $this->cat_name = Html::encode($this->categories->title); //Виборка всех статей категории сорт. по дате в одратном порядке $this->articles = Articles::find()->where(['cat' => $this->cat_id])->orderBy(['date' => SORT_DESC])->all(); //.. проверка успешности операции if (!$this->articles) { $this->error_code = 2; } } }
public function actionSearch($req) { $query = Articles::find(); $query->andFilterWhere(['like', 'intro', $req])->orFilterWhere(['like', 'title', $req])->orFilterWhere(['like', 'description', $req]); $pagination = new Pagination(['defaultPageSize' => 5, 'totalCount' => $query->count()]); if ($query->count() > 0) { return $this->render('results', ['results' => $query->all(), 'pagination' => $pagination]); } else { Yii::$app->session->setFlash('error', 'Sorry, we are unable to find anything for your request.'); return $this->goHome(); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Articles::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, 'date' => $this->date, 'cat' => $this->cat, 'author' => $this->author, 'slider' => $this->slider, 'video' => $this->video]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'img', $this->img])->andFilterWhere(['like', 'intro', $this->intro])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
public function init() { parent::init(); $this->error_code = 0; // Проверка наличия в базе категории с заданим id $this->articles = Articles::find()->where(['id' => $this->art_id])->one(); $this->categories = Categories::find()->where(['id' => $this->articles->cat])->one(); if (!$this->articles) { $this->error_code = 3; } if (!$this->categories) { $this->error_code = 4; } }