我們知道,thinkphp的拓展案例blog,只告訴我們怎樣去添加標(biāo)簽tag,卻沒有刪除和更新標(biāo)簽的方法,我在前面的《怎樣徹底刪除thinkphp案例blog的標(biāo)簽?》為拓展案例blog寫了一個刪除標(biāo)簽的方法,接下來將寫一個標(biāo)簽的更新方法.
一般情況下,我們寫博客后,很少去改動標(biāo)簽了,但是如果我們改動標(biāo)簽如,刪除,添加,減少標(biāo)簽怎么辦呢?這無疑造成think_tag和think_tagged兩個表垃圾信息的積累,好了,言歸正轉(zhuǎn).
在更新標(biāo)簽時我們來了解兩個參數(shù):
$oldtags:更新前,存在thinphp_tag表中標(biāo)簽
$newstags:更新時,插入thinphp_tag之前,表單提交過來的標(biāo)簽
更新文章時,標(biāo)簽可能會有以下幾種變化:
1、$newstags與$oldtags部分相同-> 添加或減少或修改標(biāo)簽,標(biāo)簽的個數(shù)和名稱和原來部分相同。
2、$newstags與$oldtags完全相同->不修改標(biāo)簽
3、$newstags與$oldtags完全不同 ->添加或減少標(biāo)簽,并且標(biāo)簽的個數(shù)和名稱和原來完全不同
對于2我們只要通過判斷比較過濾即可,對于1、3通過判斷比較后,再作刪除或添加處理:
刪除:要刪除的標(biāo)簽名,在thinphp_tag已存在,當(dāng)標(biāo)簽的count=1時,就把它刪除掉,當(dāng)count>1時,就減少1(count-1).
添加:要添加的標(biāo)簽名稱,如果thinphp_tag表中已存在則count(count >1)在原來的基礎(chǔ)上+1即count+1,如果不存在(count =0),則添加新標(biāo)簽,count+1.具體函數(shù)如下:
- public function updateTag($vo,$module) {
- $recordId= trim($vo['id']);
- if($vo['keywords']==''){//如果沒有標(biāo)簽,則把原來的標(biāo)簽刪除
- $this->deltag($recordId);
- }else{
- $newtags = explode(' ', $vo['keywords']);//獲取更新的標(biāo)簽并轉(zhuǎn)為數(shù)組(keywords是這里的標(biāo)簽字段,在thinkphp的拓展案例blog為tags,注意)
- $condition['recordId'] = $recordId;//當(dāng)有多個標(biāo)簽時,將查詢多篇日記,而不是一篇
- $tagged=M('Tagged');
- $tag=M('Tag');
- $taggedlist= $tagged->where($condition)->select();
- if($taggedlist !==false){
- foreach ($taggedlist as $key => $value) {
- $tagId=trim($value['tagId']);
- $tagvo=$tag->where('id='.$tagId)->find();
- $oldtags[]=$tagvo['name'];//獲取原來的標(biāo)簽
- }
- $result=count(array_diff(array_diff($newtags,$oldtags),array_diff($oldtags,$newtags)));
- $result1=count(array_diff($newtags,$oldtags));//返回更新前后TAG的差值數(shù)
- $result2=count(array_diff($oldtags,$newtags));//返回更新前后TAG的差值數(shù)
- if(($result1 !== $result2) || ($result !==0)){//2與原來的完全相同->過濾掉
- $array_intersect=array_intersect($oldtags,$newtags);//取得交值
- $oldtags_diff=array_diff($oldtags,$array_intersect);//原來的標(biāo)簽,被更新后已無用,需要刪除的
- $newtags_diff=array_diff($newtags,$array_intersect);//修改的標(biāo)簽,需要添加的
- //刪除或者count-1
- if(count($oldtags_diff) !==0){
- foreach ($oldtags_diff as $name) {
- $tagvo=$tag->where("module='$module' and name='$name'")->find();
- $count=intval($tagvo['count']);//獲取標(biāo)簽的數(shù)量
- if($count==1){
- $tag->where('id='.$tagvo['id'])->delete();
- $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete();
- }elseif($count > 1){
- $tag->where('id='.$tagvo['id'])->setDec('count',1);//標(biāo)簽數(shù)量減1
- $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete();//刪除tagged中相關(guān)數(shù)據(jù)
- }
- }
- }
- //添加更新的標(biāo)簽
- if(count($newtags_diff) !==0){
- foreach ($newtags_diff as $v) {
- $v = trim($v);
- if (!emptyempty($v)) {
- // 記錄已經(jīng)存在的標(biāo)簽
- $map['module'] = $module;
- $map['name'] = $v;
- $tagg = $tag->where($map)->find();
- if ($tagg) {//如果現(xiàn)在保存的標(biāo)簽與之前相同的標(biāo)簽累加
- $tagId = $tagg['id'];
- $tag->where('id=' . $tagg["id"])->setInc('count', 1);//count統(tǒng)計加1(這個函數(shù)有三個參數(shù),默認(rèn)加1)
- } else {//如果是新添的標(biāo)簽,標(biāo)簽+1
- $t = array();
- $t["name"] = $v;
- $t["count"] = 1;
- $t["module"] = $module;
- $result = $tag->add($t);
- $tagId = $result;
- }
- }
- //記錄tag信息
- $t = array();
- $t["module"] = $module;
- $t["recordId"] = $recordId;//保存news的ID
- $t["tagTime"] = time();
- $t["tagId"] = $tagId;//保存tag的ID
- $tagged->add($t);
- }
- }
- }
- }
- }
- }
使用方法:
- public function update() {
- $Blog=D('Blog');
- $vo=$Blog->create();
- $this->updateTag($vo,'Blog');//更新前調(diào)用
- if (false === $vo) {
- $this->error($Blog->getError());
- }
- // 更新數(shù)據(jù)
- $list = $Blog->save();
- if (false !== $list) {
- //print_r($list);
- $this->success('編輯成功!');
- } else {
- //錯誤提示
- $this->error('編輯失敗!');
- }
- }
新聞熱點
疑難解答
圖片精選