Example #1
0
 /**
  * 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('blog')->userClass;
     /* uncomment to sort by relations table on respective column
     		$dataProvider->sort->attributes['blogcatposId'] = [			
     			'asc' => ['{{%blogcatpos}}.id' => SORT_ASC],
     			'desc' => ['{{%blogcatpos}}.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']]);
     foreach ($params as $p) {
         $query->andFilterWhere($p);
     }
     $params = self::queryString([['title'], ['description'], ['content'], ['tags'], ['image']]);
     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;
         $dsn = $this->db->dsn;
         $cquery = $this->find();
         if (strtolower(substr($dsn, 0, 5)) == "mysql") {
             $cquery->select(["GROUP_CONCAT(" . $this->tableName() . ".id)"]);
         } else {
             $cquery->select(["array_agg(" . $this->tableName() . ".id)"]);
         }
         $cquery->leftJoin(BlogCatPos::tableName() . " as cp", $this->tableName() . ".id = cp.post_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(content) like '%" . strtolower($this->term) . "%'", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]]]]);
         }
     }
     return $dataProvider;
 }