《PHP學(xué)習(xí):徹底刪除thinkphp3.1案例blog標(biāo)簽的方法》要點(diǎn):
本文介紹了PHP學(xué)習(xí):徹底刪除thinkphp3.1案例blog標(biāo)簽的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP教程本文實(shí)例講述了徹底刪除thinkphp3.1案例blog標(biāo)簽的辦法.分享給大家供大家參考.具體辦法如下:
PHP教程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)系.
PHP教程函數(shù)如下:
代碼如下:
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)簽,所以這里對(duì)$tagIds作一下遍歷
? foreach ($tagIds as $tagIdk => $tagIdv) {
????????????
?? $tagId=$tagIdv;??
??????????????????
?? $tag=D('Tag');
???????????????????
?? $tagvo=$tag->where('id='.$tagId)->find();//獲取每個(gè)$tagId對(duì)應(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();
???????????????????
??? }
}
PHP教程函數(shù)寫好了,怎么使用呢?辦法很簡單.
我們來看一下刪除日記的函數(shù)
代碼如下:
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_'));
??????????? }
??????? }
}
PHP教程這個(gè)函數(shù)是放在Examples\Blog\Lib\Action\PublicAction.class.php這個(gè)公共類里的,BlogAction.class.php類繼承了其刪除函數(shù),我們就把deltag($recordId)函數(shù)放在delete() 里調(diào)用,如下:
代碼如下:
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_'));
???? }
?}
}
PHP教程以上只適用刪除單條日記的情況,當(dāng)然如要批量刪除日記,只要遍歷刪除blog的ID同時(shí)調(diào)用一下deltag($recordId)就OK了.
PHP教程?希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所贊助.
《PHP學(xué)習(xí):徹底刪除thinkphp3.1案例blog標(biāo)簽的方法》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP學(xué)習(xí):徹底刪除thinkphp3.1案例blog標(biāo)簽的方法》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/13456.html