/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ArticleSearch::find(); $query->joinWith(['category']); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $dataProvider->sort->attributes['category_title'] = ['asc' => [Category::tableName() . '.title' => SORT_ASC], 'desc' => [Category::tableName() . '.title' => SORT_DESC]]; $query->andFilterWhere(['id' => $this->id, Article::tableName() . '.is_active' => $this->is_active, 'is_comment_enabled' => $this->is_comment_enabled, 'category_id' => $this->category_id, 'timestamp' => $this->timestamp]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', Category::tableName() . '.title', $this->category_title])->andFilterWhere(['like', 'html_title', $this->html_title])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'key_words', $this->key_words]); return $dataProvider; }
/** * Все * * @param null $q * @param null $id * @return array */ public function actionIndex($q = null, $id = null) { Yii::$app->response->format = Response::FORMAT_JSON; $out = ['results' => ['id' => '', 'text' => '']]; if (!is_null($q)) { $query = new Query(); $query->select('id, name')->from(Category::tableName())->where(['like', 'name', $q])->andWhere(['status' => Category::STATUS_ACTIVE])->limit(20); $command = $query->createCommand(); $data = $command->queryAll(); $categories = []; foreach ($data as &$category) { $categories[] = ['id' => $category['id'], 'text' => $category['name']]; } $out['results'] = $categories; } else { if ($id > 0) { $category = Category::findById($id, Category::STATUS_ACTIVE); if ($category) { $out['results'] = ['id' => $category->id, 'text' => $category->name]; } } } return $out; }
/** * @param int $state * @return $this */ public function status($state = Category::STATUS_ACTIVE) { $this->andWhere([Category::tableName() . '.status' => $state]); return $this; }