Пример #1
0
 /**
  * Creates data provider instance with search query applied
  * @return ActiveDataProvider
  */
 public function search($params = [], $pageSize = 16)
 {
     // print_r($params);exit;
     // $params['attrs.tag.tag_name'] = 'zhanglu';
     // print_r($params);exit;
     $query = self::find()->select(['i_news.*', "GROUP_CONCAT(tag.tag_name SEPARATOR ',') as tag_names"])->where(['news_active' => 0])->joinWith(['attrs' => function ($query) {
         $query->from(['attrs' => '{{%news_attr_value}}'])->onCondition(['attrs.attr_id' => PostAttribute::TAG])->joinWith(['tag' => function ($query) {
             $query->from(['tag' => '{{%tag}}']);
         }]);
         // ->select([
         //     PostAttrVal::tableName() . '.news_id',
         //     PostAttrVal::tableName() . '.attr_id',
         //     PostAttrVal::tableName() . '.attr_value_text'
         // ]);
     }])->groupBy("{{%news}}.news_id");
     // ->where(['and', 'poll_active=0', ['or', 'parent_id is null', 'parent_id = 0']]);
     // if (count($params) == 0) {
     //     $query->orderBy(['news_id' => SORT_DESC])->limit(3);
     // }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $pageSize], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC, 'news_id' => SORT_DESC]]]);
     // $dataProvider->sort->attributes['attrs.attr_value_text'] = [
     //     'asc' => ['attr_value_text' => SORT_ASC],
     //     'desc' => ['attr_value_text' => SORT_DESC],
     // ];
     $dataProvider->sort->attributes['attrs.tag.tag_name'] = ['asc' => ['tag.tag_name' => SORT_ASC], 'desc' => ['tag.tag_name' => SORT_DESC]];
     // print_r($params);exit;
     // return $dataProvider;
     if (!($this->load($params) && $this->validate())) {
         // echo "string";exit;
         return $dataProvider;
     }
     $query->andFilterWhere(['like', Post::tableName() . '.news_id', $this->news_id])->andFilterWhere(['like', 'news_title', $this->news_title])->andFilterWhere(['like', 'news_author', $this->news_author])->andFilterWhere(['like', 'news_from', $this->news_from])->andFilterWhere(['like', 'tag.tag_name', $this->getAttribute('attrs.tag.tag_name')]);
     // $query->orderBy(['news_id' => SORT_DESC])->limit(3);
     // if (count($params) == 0) {
     // }
     return $dataProvider;
 }