thinkphp3.1案例blog的标签添加一个删除方法

浏览:1778 发布日期:2012/12/21 分类:技术分享
thinkphp3.1框架中的案例blog,添加日记的同时可以添加标签tag,但仅此而已。当删除日记时,标签并没有被删除掉,从而造成think_tagged表和think_tag累积了垃圾数据。为了实现删除日记的同时也一起清理掉think_tagged表和think_tag那些过时的数据,我写了一个函数,在看下面函数时,要先弄清think_tagged表、think_tag和think_blog表的关联关系。

函数如下:    public function deltag($recordId){            
       
          $condition['recordId'] = $recordId;//获取日记的ID
                 
         $tagged=M('Tagged');
         $taggedlist= $tagged->where($condition)->select();//这里用select而不用find,因为一篇日记可能有多个标签
                      
       $taggedids=array();//声明一个数组,用来装think_tagged表的ID
                      
        $tagIds=array();//声明一个数组,用来装think_tag表的ID
                      
        foreach ($taggedlist as $key => $value) {
                      
       $tagIds[]=$value['tagId'];//获取think_tag表的ID
                                 
       $taggedids[]=$value['id'];//获取think_tagged表的ID
                               }
    //考虑到一篇日记可能有多个标签,所以这里对$tagIds作一下遍历
      foreach ($tagIds as $tagIdk => $tagIdv) {
                      
       $tagId=$tagIdv;        
                                       
       $tag=D('Tag');
                              
       $tagvo=$tag->where('id='.$tagId)->find();//获取每个$tagId对应的一条记录
                      
      $count=intval($tagvo['count']);//获取标签的数量
                      
      if($count==1){//如果$count==1,说明这个标签仅有这篇日记所有,删掉。
                              
      $tag->where('id='.$tagId)->delete();
                                   
      }elseif($count > 1){//$count > 1,说明这个标签为多篇日记所有,不能删除,所以减1。
                                   
      $tag->where('id='.$tagId)->setDec('count',1);//setDec使$count减1,注意thinkphp3.1的使用方法。
                              
       }
    }
    //下面是删除日记存在think_tagged表里的相关数据
       foreach ($taggedids as $taggedid_k => $taggedid_v) {
                             
        $tagged->where('id='.$taggedid_v)->delete();
                                 
        }
                      
                      
    }
函数写好了,怎么使用呢?方法很简单。
我们来看一下thinkphp3.1案例blog中删除日记的函数    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_'));
                }
            }
        }  
这个函数是放在Examples\Blog\Lib\Action\PublicAction.class.php这个公共类里的,BlogAction.class.php类继承了其删除函数,我们就把deltag($recordId)函数放在delete() 里调用,如下:    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_'));
                }
            }
        }  
以上只适用删除单条日记的情况,当然如要批量删除日记,只要遍历删除blog的ID同时调用一下deltag($recordId)就OK了。
最佳答案
评论() 相关
后面还有条评论,点击查看>>