課程
/后端開發(fā)
/PHP
/PHP之Yii2框架搭建完整博客系統(tǒng)
求一個具體點(diǎn)的思路
2017-03-16
源自:PHP之Yii2框架搭建完整博客系統(tǒng) 3-22
正在回答
<?php namespace?frontend\widgets\post; /** ?*?文章列表組件 ?*/ use?common\models\PostModel; use?common\models\RelationPostTagsModel; use?common\models\TagsModel; use?frontend\models\TagForm; use?frontend\models\PostForm; use?Yii; use?yii\base\Widget; use?yii\data\Pagination; use?yii\db\Query; use?yii\helpers\Url; class?PostWidget?extends?Widget { ????/** ?????*?文章列表的標(biāo)題 ?????*?@var?string ?????*/ ????public?$title?=?''; ????/** ?????*?顯示條數(shù) ?????*?@var?int ?????*/ ????public?$limit?=?3; ????/** ?????*?是否顯示更多 ?????*?@var?bool ?????*/ ????public?$more?=?true; ????/** ?????*?是否顯示分頁 ?????*?@var?bool ?????*/ ????public?$page?=?true; ????public?function?run() ????{ ????????$tag?=?Yii::$app->request->get('tag'); ????????$curPage?=?Yii::$app->request->get('post',1); ????????//查詢條件 ????????if(!empty($tag)){ ????????????$ids?=?[]; ????????????//方法一:使用with關(guān)聯(lián)表 ????????????//$tags?=?new?TagForm(); ????????????//$ids?=?$tags->getIdsByTag(['tag_name'=>$tag]); ????????????//方法二:先查詢標(biāo)簽id,再查詢文章id數(shù)組 ????????????//$data?=?TagsModel::find()->where(['tag_name'=>$tag])->asArray()->one(); ????????????//$ids?=?RelationPostTagsModel::getPostIds($data['id']); ????????????//方法三:使用join關(guān)聯(lián)表 ????????????$data?=?(new?Query()) ????????????????->select('a.post_id') ????????????????->from(['a'=>RelationPostTagsModel::tableName()]) ????????????????->join('LEFT?JOIN',['b'=>TagsModel::tableName()],'a.tag_id=b.id') ????????????????->where(['b.tag_name'=>$tag]) ????????????????->orderBy(['a.post_id'=>SORT_DESC]) ????????????????->all(); ????????????if(!empty($data)){ ????????????????foreach?($data?as?$value){ ????????????????????$ids[]?=?$value['post_id']; ????????????????} ????????????} ????????????//print_r($ids);exit; ????????????$cond?=?['id'=>$ids,'is_valid'=>PostModel::IS_VALID]; ????????}else{ ????????????$cond?=?['=','is_valid',PostModel::IS_VALID]; ????????} ????????$res?=?PostForm::getList($cond,$curPage,$this->limit); ????????$result['title']?=?$this->title?:'最新文章';//組件標(biāo)題支持自定義 ????????$result['more']?=?Url::to(['post/index']); ????????$result['body']=?$res['data']?:[]; ????????//是否顯示分頁 ????????if($this->page) ????????{ ????????????$pages?=?new?Pagination(['totalCount'=>$res['count'],'pageSize'=>$res['pageSize']]); ????????????$result['page']?=?$pages; ????????} ????????return?$this->render('index',['data'=>$result]); ????} }
//文章列表組建里面 $curPage?=?\Yii::$app->request->get('page',?1); $tagName?????=?\Yii::$app->request->get('tag'); $cond????=?[]; if?($tagName)?{ ????$tags????=?new?Tags(); ????$tag?=?$tags ????????->find() ????????->with('postid') ????????->where(['tag_name'=>$tagName]) ????????->asArray() ????????->one(); ????if?(isset($tag['postid'])?&&?!empty($tag['postid']))?{ ????????foreach?($tag['postid']?as?$v)?{ ????????????$id[]?=?$v['post_id']; ????????} ????????$cond?=?['id'?=>?$id,?'is_valid'?=>?Posts::IS_VALID]; ????} }
//?tagModel數(shù)據(jù)模型類 public?function?getPostid() { ????return?$this->hasMany(RelationPostTags::className(),?['tag_id'=>'id']); }
請問下也是放在hotwidget里嗎
ptmodel是什么
在組件里添加條件。
$tags = Yii::$app->request->get('tag');if($tags) { ? ?$tag = new PtModel(); ? ?$post_id = $tag->getPostid($tags); ? ?if ($post_id) { ? ? ? ?foreach ($post_id as $v) { ? ? ? ? ? ?$id[] = $v['post_id']; ? ? ? ?} ? ? ? ?$cond = ['id' => $id,'is_valid'=>PostsModel::IS_VALID]; ? ?}}
$res = PostsModel::getList($cond,$curPage,$this->limit);
/*關(guān)聯(lián)表*/
public function getPostid($id){ ? ?$res = PtModel::find()->select('post_id')->where('tag_id ='.$id)->asArray()->all(); ? ?return $res;}
舉報
Yii是PHP快速開發(fā)的最佳實(shí)踐之一,一起領(lǐng)略yii2快速開發(fā)的風(fēng)采
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2020-03-19
2019-03-23
2018-09-21
請問下也是放在hotwidget里嗎
2017-03-29
ptmodel是什么
2017-03-29
在組件里添加條件。
$tags = Yii::$app->request->get('tag');
if($tags) {
? ?$tag = new PtModel();
? ?$post_id = $tag->getPostid($tags);
? ?if ($post_id) {
? ? ? ?foreach ($post_id as $v) {
? ? ? ? ? ?$id[] = $v['post_id'];
? ? ? ?}
? ? ? ?$cond = ['id' => $id,'is_valid'=>PostsModel::IS_VALID];
? ?}
}
$res = PostsModel::getList($cond,$curPage,$this->limit);
/*關(guān)聯(lián)表*/
public function getPostid($id){
? ?$res = PtModel::find()->select('post_id')->where('tag_id ='.$id)->asArray()->all();
? ?return $res;
}