/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['author']); $dataProvider = new ActiveDataProvider(['query' => $query]); $userClass = Yii::$app->getModule('yes')->userClass; /* uncomment to sort by relations table on respective column $dataProvider->sort->attributes['salesId'] = [ 'asc' => ['{{%sales}}.id' => SORT_ASC], 'desc' => ['{{%sales}}.id' => SORT_DESC], ]; $dataProvider->sort->attributes['catprosId'] = [ 'asc' => ['{{%catpros}}.id' => SORT_ASC], 'desc' => ['{{%catpros}}.id' => SORT_DESC], ];*/ $dataProvider->sort->attributes['authorName'] = ['asc' => [$userClass::tableName() . '.username' => SORT_ASC], 'desc' => [$userClass::tableName() . '.username' => SORT_DESC]]; $dataProvider->sort->attributes['term'] = ['asc' => ['title' => SORT_ASC], 'desc' => ['title' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['status' => $this->status, 'isdel' => $this->isdel, 'isfeatured' => $this->isfeatured]); $params = self::queryNumber([['id', $this->tableName()], ['author_id'], ['price'], ['discount']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['title'], ['sku'], ['description'], ['content'], ['data'], ['tags'], ['images']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryTime([['time']]); foreach ($params as $p) { $query->andFilterWhere($p); } $query->andFilterWhere(['like', 'lower(' . $userClass::tableName() . '.username)', strtolower($this->authorName)]); if ($this->category || $this->term) { $term = $this->term ? $this->term : $this->category; $cquery = $this->find()->select(["array_agg(" . $this->tableName() . ".id)"])->leftJoin(CatPro::tableName() . " as cp", $this->tableName() . ".id = cp.product_id")->leftJoin(Category::tableName() . " as c", "cp.category_id = c.id"); if ($this->category) { $cquery->andWhere("lower(c.title) = '" . strtolower($term) . "'"); } else { $cquery->andWhere("lower(c.title) like '%" . strtolower($term) . "%' or lower(c.description) like '%" . strtolower($term) . "%'"); } $res = $cquery->scalar(); $res = $res == "" ? "{}" : $res; if ($this->category) { //$query->andFilterWhere(["OR","false",$this->tableName().".id = ANY ('".$res."')"]); $query->andFilterWhere(["OR", "false", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]); } else { $query->andFilterWhere(["OR", "lower(title) like '%" . strtolower($this->term) . "%'", ["OR", "lower(description) like '%" . strtolower($this->term) . "%'", ["OR", "lower(tags) like '%" . strtolower($this->term) . "%'", ["OR", "lower(sku) like '%" . strtolower($this->term) . "%'", ["OR", "lower(content) like '%" . strtolower($this->term) . "%'", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]]]]]); } } return $dataProvider; }
public static function find() { return parent::find()->where([Category::tableName() . '.isdel' => 0]); }