public function run() { $post = Post::find(); //选择栏目 if (!empty($this->short_code)) { $post = $post->select(Post::tableName() . '.*')->joinWith(['column' => function ($query) { $query->where([Column::tableName() . '.short_code' => $this->short_code]); }]); } //选择类型 if (!empty($this->type)) { if ($this->type == static::TYPE_N) { //自动取新 $post = $post->orderBy(Post::tableName() . '.created_at DESC'); } elseif ($this->type == static::TYPE_V) { //自动取查看最多 $post = $post->orderBy(Post::tableName() . '.hits DESC'); } else { //后台指定 $post = $post->andFilterWhere(['like', Post::tableName() . '.flag', $this->type]); } } //限制数量 $models = $post->active()->limit($this->num)->all(); // fb($models); return $this->render('side-post-top', ['title' => $this->title, 'length' => $this->length, 'models' => $models]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Post::find()->alive(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => [], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'column_id' => $this->column_id, 'hits' => $this->hits, 'status' => $this->status, 'deleted' => $this->deleted, 'updated_at' => $this->updated_at, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'flag', $this->flag])->andFilterWhere(['like', 'colorval', $this->colorval])->andFilterWhere(['like', 'boldval', $this->boldval])->andFilterWhere(['like', 'source', $this->source])->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'linkurl', $this->linkurl])->andFilterWhere(['like', 'keywords', $this->keywords])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'pic_url', $this->pic_url])->andFilterWhere(['like', 'picarr', $this->picarr]); return $dataProvider; }