Home Backend Development PHP Tutorial 彻底删除thinkphp3.1案例blog标签的方法_php实例

彻底删除thinkphp3.1案例blog标签的方法_php实例

Jun 07, 2016 pm 05:14 PM
thinkphp delete Label

本文实例讲述了彻底删除thinkphp3.1案例blog标签的方法。分享给大家供大家参考。具体方法如下:

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();
                   
    }
}

函数写好了,怎么使用呢?方法很简单。
我们来看一下删除日记的函数

复制代码 代码如下:
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了。

 希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Is it true that you can be blocked and deleted on WeChat and permanently unable to be added? Is it true that you can be blocked and deleted on WeChat and permanently unable to be added? Apr 08, 2024 am 11:41 AM

1. First of all, it is false to block and delete someone permanently and not add them permanently. If you want to add the other party after you have blocked them and deleted them, you only need the other party's consent. 2. If a user blocks someone, the other party will not be able to send messages to the user, view the user's circle of friends, or make calls with the user. 3. Blocking does not mean deleting the other party from the user's WeChat contact list. 4. If the user deletes the other party from the user's WeChat contact list after blocking them, there is no way to recover after deletion. 5. If the user wants to add the other party as a friend again, the other party needs to agree and add the user again.

How to delete Xiaohongshu releases? How to recover after deletion? How to delete Xiaohongshu releases? How to recover after deletion? Mar 21, 2024 pm 05:10 PM

As a popular social e-commerce platform, Xiaohongshu has attracted a large number of users to share their daily life and shopping experiences. Sometimes we may inadvertently publish some inappropriate content, which needs to be deleted in time to better maintain our personal image or comply with platform regulations. 1. How to delete Xiaohongshu releases? 1. Log in to your Xiaohongshu account and enter your personal homepage. 2. At the bottom of the personal homepage, find the "My Creations" option and click to enter. 3. On the "My Creations" page, you can see all published content, including notes, videos, etc. 4. Find the content that needs to be deleted and click the "..." button on the right. 5. In the pop-up menu, select the "Delete" option. 6. After confirming the deletion, the content will disappear from your personal homepage and public page.

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to completely delete TikTok chat history How to completely delete TikTok chat history May 07, 2024 am 11:14 AM

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to send files to others on TikTok? How to delete files sent to others? How to send files to others on TikTok? How to delete files sent to others? Mar 22, 2024 am 08:30 AM

On Douyin, users can not only share their life details and talents, but also interact with other users. In this process, sometimes we need to send files to other users, such as pictures, videos, etc. So, how to send files to others on Douyin? 1. How to send files to others on Douyin? 1. Open Douyin and enter the chat interface where you want to send files. 2. Click the "+" sign in the chat interface and select "File". 3. In the file options, you can choose to send pictures, videos, audio and other files. After selecting the file you want to send, click "Send". 4. Wait for the other party to accept your file. Once the other party accepts it, the file will be transferred successfully. 2. How to delete files sent to others on Douyin? 1. Open Douyin and enter the text you sent.

How can I retrieve someone else's deleted comment on Xiaohongshu? Will it be displayed if someone else's comment is deleted? How can I retrieve someone else's deleted comment on Xiaohongshu? Will it be displayed if someone else's comment is deleted? Mar 21, 2024 pm 10:46 PM

Xiaohongshu is a popular social e-commerce platform, and interactive comments between users are an indispensable method of communication on the platform. Occasionally, we may find that our comments have been deleted by others, which can be confusing. 1. How can I retrieve someone else’s deleted comments on Xiaohongshu? When you find that your comments have been deleted, you can first try to directly search for relevant posts or products on the platform to see if you can still find the comment. If the comment is still displayed after being deleted, it may have been deleted by the original post owner. At this time, you can try to contact the original post owner to ask the reason for deleting the comment and request to restore the comment. If a comment has been completely deleted and cannot be found on the original post, the chances of it being reinstated on the platform are relatively slim. You can try other ways

See all articles