Completely delete thinkphp3.1 Case Blog Tag Method _php instance

Source: Internet
Author: User
This article describes the method of completely deleting the thinkphp3.1 case blog tag. Share to everyone for your reference. Here's how:

thinkphp3.1 in the framework of the case blog, add a journal can also add tags tag tag, but that's all. When the journal is deleted, the tags are not deleted, resulting in think_tagged tables and think_tag accumulating junk data. In order to delete the diary and also clean up the think_tagged table and think_tag Those outdated data, I wrote a function, in the following function, we should first understand the think_tagged table, Think_tag and Think_blog Table Association.

The functions are as follows:
Copy CodeThe code is as follows: Public function Deltag ($recordId) {

$condition [' recordId '] = $recordId;//Get Journal ID

$tagged =m (' tagged ');
$taggedlist = $tagged->where ($condition)->select ();//Use Select instead of Find, because a journal may have multiple labels

$taggedids =array ();//declares an array that is used to mount the ID of the think_tagged table

$tagIds =array ();//declares an array that is used to mount the ID of the Think_tag table

foreach ($taggedlist as $key = = $value) {

$tagIds []= $value [' TagId '];//gets the ID of the Think_tag table

$taggedids []= $value [' id '];//gets the ID of the think_tagged table
}
Consider that a journal may have multiple labels, so here's a walk through the $tagids
foreach ($tagIds as $TAGIDK = = $TAGIDV) {

$tagId = $TAGIDV;

$tag =d (' tag ');

$tagvo = $tag->where (' id= '. $tagId)->find ();//Get a record for each $tagid

$count =intval ($tagvo [' count ']);//Get the number of labels

if ($count ==1) {//If $count==1, this label only has this diary all, delete.

$tag->where (' id= '. $tagId)->delete ();

}elseif ($count > 1) {//$count > 1, indicating that this label is for multiple diaries and cannot be deleted, so minus 1.

$tag->where (' id= ' $tagId)->setdec (' Count ', 1);//setdec $count minus 1, pay attention to how thinkphp3.1 is used.

}
}
The following is the deletion of the diary of the existing Think_tagged table related data
foreach ($taggedids as $taggedid _k = $taggedid _v) {

$tagged->where (' id= '. $taggedid _v)->delete ();

}
}

The

function is well written, how to use it? The method is simple.
Let's look at the function to delete the journal
Copy the code code as follows: Public function Delete () {
//delete 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 placed in the public class of examples\blog\lib\action\publicaction.class.php, BlogAction.class.php class inherits its delete function, we put Deltag ($ RECORDID) function is placed in the delete () call, as follows:
Copy the Code Code 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 delete a single diary, of course, if you want to delete the diary in bulk, as long as the removal of the ID of the blog at the same time call Deltag ($recordId) ok.

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

  • Related Article

    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.