common\models\PostSearch::search PHP Method

    public function search($params)
    {
        $query = Post::find()->with('user', 'category');
        // 如果有无人区节点 帖子列表过滤无人区节点的帖子
        if (PostMeta::noManLandId() && (empty($params['PostSearch']['post_meta_id']) || $params['PostSearch']['post_meta_id'] != PostMeta::noManLandId())) {
            $query->andWhere(['!=', 'post_meta_id', PostMeta::noManLandId()]);
        }
        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 20], 'sort' => ['defaultOrder' => ['order' => SORT_ASC, 'last_comment_time' => SORT_DESC, 'created_at' => SORT_DESC]]]);
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        $query->andFilterWhere(['id' => $this->id, 'post_meta_id' => $this->post_meta_id, 'user_id' => $this->user_id, 'view_count' => $this->view_count, 'comment_count' => $this->comment_count, 'favorite_count' => $this->favorite_count, 'like_count' => $this->like_count, 'thanks_count' => $this->thanks_count, 'hate_count' => $this->hate_count, 'status' => $this->status, 'order' => $this->order, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
        $query->andFilterWhere(['like', 'type', $this->type])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'excerpt', $this->excerpt])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'tags', $this->tags]);
        return $dataProvider;
    }

Usage Example

Example #1
0
 /**
  * Lists all Post models.
  * @return mixed
  */
 public function actionIndex($id = 0)
 {
     if ($id > 0) {
         $query = Post::find()->where(['category' => $id]);
         //die(print_r($query));
         $pages = new Pagination(['totalCount' => Post::find()->where(['category' => $id])->count(), 'pageSize' => 8]);
         $posts = $query->offset($pages->offset)->limit($pages->limit)->all();
         $all_posts = new ActiveDataProvider(['query' => $query->offset($pages->offset)->limit($pages->limit)->all()]);
         /*$all_posts = new ActiveDataProvider([
               'query' => Post::find(),
               'pagination' => [
                   'totalCount' => $countQuery->count(),
                   'pageSize' => 5
               ],
           ]);*/
         return $this->render('index', ['allPosts' => $all_posts, 'pages' => $pages, 'posts' => $posts]);
     } else {
         $searchModel = new PostSearch();
         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
         $query = Post::find();
         $countQuery = clone $query;
         $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 8]);
         $posts = $query->offset($pages->offset)->limit($pages->limit)->all();
         $all_posts = new ActiveDataProvider(['query' => $query->offset($pages->offset)->limit($pages->limit)->all()]);
         /*$all_posts = new ActiveDataProvider([
               'query' => Post::find(),
               'pagination' => [
                   'totalCount' => $countQuery->count(),
                   'pageSize' => 5
               ],
           ]);*/
         return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'allPosts' => $all_posts, 'pages' => $pages, 'posts' => $posts]);
     }
 }
All Usage Examples Of common\models\PostSearch::search