/** * Creates data provider instance with search query applied * * @param array $params * @param int $taxonomy_id * * @return ActiveDataProvider */ public function search($params, $taxonomy_id) { $query = TermModel::find(); $query->andWhere(['taxonomy_id' => $taxonomy_id]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'taxonomy_id' => $this->taxonomy_id, 'term_parent' => $this->term_parent, 'term_count' => $this->term_count]); $query->andFilterWhere(['like', 'term_name', $this->term_name])->andFilterWhere(['like', 'term_slug', $this->term_slug])->andFilterWhere(['like', 'term_description', $this->term_description]); return $dataProvider; }
/** * Ajax search for term and return json. * * @return string */ public function actionAjaxSearch() { Yii::$app->response->format = Response::FORMAT_JSON; if ($term = Yii::$app->request->post('Term')) { $model = Term::find()->select(['id', 'term_name'])->where(['like', 'term_name', $term['term_name']])->andWhere(['taxonomy_id' => $term['taxonomy_id']])->limit('10')->all(); if ($model) { return $model; } } return []; }