欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

徹底刪除thinkphp3.1案例blog標(biāo)簽的方法

 更新時(shí)間:2014年12月05日 09:43:29   投稿:shichen2014  
這篇文章主要介紹了徹底刪除thinkphp3.1案例blog標(biāo)簽的方法,以thinkphp3.1框架中的案例blog為基礎(chǔ)介紹了刪除日記時(shí)同步刪除tag冗余數(shù)據(jù)的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了徹底刪除thinkphp3.1案例blog標(biāo)簽的方法。分享給大家供大家參考。具體方法如下:

thinkphp3.1框架中的案例blog,添加日記的同時(shí)可以添加標(biāo)簽tag,但僅此而已。當(dāng)刪除日記時(shí),標(biāo)簽并沒有被刪除掉,從而造成think_tagged表和think_tag累積了垃圾數(shù)據(jù)。為了實(shí)現(xiàn)刪除日記的同時(shí)也一起清理掉think_tagged表和think_tag那些過時(shí)的數(shù)據(jù),我寫了一個(gè)函數(shù),在看下面函數(shù)時(shí),要先弄清think_tagged表、think_tag和think_blog表的關(guān)聯(lián)關(guān)系。

函數(shù)如下:

復(fù)制代碼 代碼如下:
public function deltag($recordId){      
    
      $condition['recordId'] = $recordId;//獲取日記的ID
          
     $tagged=M('Tagged');
     $taggedlist= $tagged->where($condition)->select();//這里用select而不用find,因?yàn)橐黄沼浛赡苡卸鄠€(gè)標(biāo)簽
            
   $taggedids=array();//聲明一個(gè)數(shù)組,用來裝think_tagged表的ID
            
    $tagIds=array();//聲明一個(gè)數(shù)組,用來裝think_tag表的ID
            
    foreach ($taggedlist as $key => $value) {
            
   $tagIds[]=$value['tagId'];//獲取think_tag表的ID
                   
   $taggedids[]=$value['id'];//獲取think_tagged表的ID
               }
 //考慮到一篇日記可能有多個(gè)標(biāo)簽,所以這里對$tagIds作一下遍歷
  foreach ($tagIds as $tagIdk => $tagIdv) {
            
   $tagId=$tagIdv;  
                  
   $tag=D('Tag');
                   
   $tagvo=$tag->where('id='.$tagId)->find();//獲取每個(gè)$tagId對應(yīng)的一條記錄
           
  $count=intval($tagvo['count']);//獲取標(biāo)簽的數(shù)量
           
  if($count==1){//如果$count==1,說明這個(gè)標(biāo)簽僅有這篇日記所有,刪掉。
                   
  $tag->where('id='.$tagId)->delete();
                    
  }elseif($count > 1){//$count > 1,說明這個(gè)標(biāo)簽為多篇日記所有,不能刪除,所以減1。
                 
  $tag->where('id='.$tagId)->setDec('count',1);//setDec使$count減1,注意thinkphp3.1的使用方法。
                
   }
 }
 //下面是刪除日記存在think_tagged表里的相關(guān)數(shù)據(jù)
   foreach ($taggedids as $taggedid_k => $taggedid_v) {
              
    $tagged->where('id='.$taggedid_v)->delete();
                   
    }
}

函數(shù)寫好了,怎么使用呢?方法很簡單。
我們來看一下刪除日記的函數(shù)

復(fù)制代碼 代碼如下:
public function delete() {
        //刪除指定記錄
        $model = M("Blog");
        if (!empty($model)) {
            $id = $_REQUEST[$model->getPk()];
            if (isset($id)) {
 
                if ($model->where("id=" . $id)->delete()) {
                    if ($this->__get('ajax')) {
                        $this->ajaxReturn($id, L('_DELETE_SUCCESS_'), 1);
                    } else {
                        $this->success(L('_DELETE_SUCCESS_'));
                    }
                } else {
                    $this->error(L('_DELETE_FAIL_'));
                }
            } else {
                $this->error(L('_ERROR_ACTION_'));
            }
        }
}

這個(gè)函數(shù)是放在Examples\Blog\Lib\Action\PublicAction.class.php這個(gè)公共類里的,BlogAction.class.php類繼承了其刪除函數(shù),我們就把deltag($recordId)函數(shù)放在delete() 里調(diào)用,如下:

復(fù)制代碼 代碼如下:
public function delete() {
 //刪除指定記錄
 $model = M("Blog");
 if (!empty($model)) {
     $id = $_REQUEST[$model->getPk()];
     if (isset($id)) {
     $recordId=$id;
      $this->deltag($recordId);
  if ($model->where("id=" . $id)->delete()) {
      if ($this->__get('ajax')) {
   $this->ajaxReturn($id, L('_DELETE_SUCCESS_'), 1);
      } else {
   $this->success(L('_DELETE_SUCCESS_'));
      }
  } else {
      $this->error(L('_DELETE_FAIL_'));
  }
     } else {
  $this->error(L('_ERROR_ACTION_'));
     }
 }
}

以上只適用刪除單條日記的情況,當(dāng)然如要批量刪除日記,只要遍歷刪除blog的ID同時(shí)調(diào)用一下deltag($recordId)就OK了。

 希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論