1], [['limit'], 'default', 'value' => 20], [['cx_mch_id'], 'required'], [['post_id'], 'required', 'on' => 'detail'], ]; } public function search() { if(!$this->validate()){ return $this->getModelError(); } $cat_query = PostMeta::find() ->where([ 'type' => PostMeta::TYPE_CAT, 'is_delete' => 0, 'cx_mch_id' => $this->cx_mch_id, ]) ->select('post_id,value as cat_id'); $query = Post::find()->alias('p') ->leftJoin(['rt' => RichText::tableName()],'rt.id=p.rich_text_id') ->leftJoin(['pm' => $cat_query], 'pm.post_id=p.id') ->where([ 'p.cx_mch_id' => $this->cx_mch_id, 'p.is_delete' => 0, 'p.scene' => $this->scene, ]) ->andFilterWhere([ 'OR', ['LIKE', 'rt.title', $this->keywords], ['LIKE', 'rt.author', $this->keywords], ['LIKE', 'rt.summary', $this->keywords], ]) ->andFilterWhere([ 'p.is_top' => $this->is_top, 'p.status' => $this->status, 'p.id' => $this->post_id, 'p.url_alias' => $this->url_alias, 'pm.cat_id' => $this->cat_id ]) ->select('rt.author,rt.title,rt.summary,rt.content,rt.cover_url,p.id,p.rich_text_id,p.sort,p.is_top,p.status,p.created_at,p.published_at,p.viewed_num,p.allow_read'); $count_query = clone $query; $count = $count_query->count(); $pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]); $list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['p.sort' => SORT_ASC, 'p.created_at' => SORT_DESC])->asArray()->all(); foreach ($list as $index => $item){ $item['created_at_cn'] = date('Y-m-d H:i:s',$item['created_at']); $item['published_at_cn'] = date('Y-m-d H:i:s',$item['published_at']); $item['status_cn'] = Post::getStatus($item['status']); $item['allow_read_cn'] = $item['allow_read'] == 1 ? '开放阅读' : '密码访问'; $item['cat_list'] = $this->getCatList($item['id']); if($this->scenario == 'detail') $item['content'] = SiteHelper::repairContent($item['content']); $list[$index] = $item; } //是否已经全部加载 $end_flag = $this->page > $pagination->pageCount ? true : false; return [ 'code' => 0, 'msg' => 'ok', 'data' => $list, 'count' => $count, 'page_size' => $this->limit, 'page_count' => $pagination->pageCount, 'page_no' => $this->page, 'end_flag' => $end_flag ]; } private function getCatList($post_id) { $list = PostMeta::find()->alias('pm') ->leftJoin(['c' => Cat::tableName()],'c.id=pm.value') ->where([ 'pm.is_delete' => 0, 'pm.type' => PostMeta::TYPE_CAT, 'pm.post_id' => $post_id, 'c.is_delete' => 0, 'c.type' => Cat::TYPE_POST_DEFAULT, ]) ->select('c.name,c.id as cat_id')->asArray()->all(); return $list; } }