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; } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Categories::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]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'img', $this->img])->andFilterWhere(['like', 'text', $this->text])->andFilterWhere(['like', 'ico', $this->ico]); 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; } }