How to permanently delete the thinkphp3.1 case blog tag _ php instance

Source: Internet
Author: User
This article mainly introduces how to delete the topic tag of thinkphp3.1 case study. Based on the topic of thinkphp3.1 framework, this article describes how to delete tag redundancy data synchronously when deleting a diary, which is of great practical value, for more information about how to delete the topic tag of thinkphp3.1, see the following example. Share it with you for your reference. The specific method is as follows:

In the case blog in thinkphp3.1 framework, you can add tag when adding a diary, but that's all. When the log is deleted, the tag is not deleted, resulting in the accumulation of junk data in the think_tagged table and think_tag. In order to delete the log and clear the outdated data of the think_tagged table and think_tag together, I wrote a function. when reading the following function, first, we need to understand the associations between the think_tagged table, think_tag table, and think_blog table.

The function is as follows:

The Code is as follows:

Public function deltag ($ recordId ){

$ Condition ['recordid'] = $ recordId; // get the ID of the diary

$ Tagged = M ('taged ');
$ Taggedlist = $ tagged-> where ($ condition)-> select (); // select is used here instead of find, because a diary may have multiple tags.

$ Taggedids = array (); // declare an array to hold the ID of the think_tagged table.

$ TagIds = array (); // declare an array to hold the ID of the think_tag table.

Foreach ($ taggedlist as $ key => $ value ){

$ TagIds [] = $ value ['tagid']; // obtain the ID of the think_tag table.

$ Taggedids [] = $ value ['id']; // obtain the id of the think_tagged table.
}
// Considering that a diary may have multiple tags, traverse $ tagIds
Foreach ($ tagIds as $ tagIdk => $ tagIdv ){

$ TagId = $ tagIdv;

$ Tag = D ('tag ');

$ Tagvo = $ tag-> where ('Id = '. $ tagId)-> find (); // obtain a record corresponding to each $ tagId

$ Count = intval ($ tagvo ['Count']); // obtain the number of tags

If ($ count = 1) {// if $ count = 1, this tag is only owned by this diary and deleted.

$ Tag-> where ('Id = '. $ tagId)-> delete ();

} Elseif ($ count> 1) {// $ count> 1, which indicates that this label is owned by multiple diaries and cannot be deleted. Therefore, 1 is subtracted.

$ Tag-> where ('Id = '. $ tagId)-> setDec ('Count', 1); // setDec minus $ count by 1. Pay attention to the usage of thinkphp3.1.

}
}
// Delete the data in the think_tagged table in the log.
Foreach ($ taggedids as $ taggedid_k => $ taggedid_v ){

$ Tagged-> where ('Id = '. $ taggedid_v)-> delete ();

}
}

After writing the function, how can I use it? The method is simple.
Let's take a look at the function for deleting a diary.

The Code is as follows:

Public function delete (){
// Delete a specified record
$ 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 _'));
}
}
}

This function is stored in Examples \ Blog \ Lib \ Action \ PublicAction. class. in the php public class, BlogAction. class. the php class inherits its delete function. We call the deltag ($ recordId) function in delete () as follows:

The Code is as follows:

Public function delete (){
// Delete a specified record
$ 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 _'));
}
}
}

The above only applies to deleting a single log. Of course, if you want to delete logs in batches, you only need to traverse the ID of the deleted blog and call deltag ($ recordId) at the same time.

I hope this article will help you design PHP programs based on the ThinkPHP framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.