In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to completely delete the tag in thinkphp3.1", the content of the explanation in the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, to study and learn "how to completely delete the tag in thinkphp3.1"!
For the case blog in the thinkphp3.1 framework, you can add a tag tag while adding a diary, but that's it. When the journal is deleted, the tag is not deleted, causing the think_ tagged table and think_tag to accumulate junk data. In order to delete the diary while also cleaning up the think_ tagged table and think_tag 's outdated data, I wrote a function to first figure out the relationship between the think_ tagged table, think_tag, and think_ blog table when looking at the following functions.
Functions are as follows:
The copy code is as follows:
Public function deltag ($recordId) {
$condition ['recordId'] = $recordId;// to get the ID of the diary
$tagged=M ('Tagged')
$taggedlist= $tagged- > where ($condition)-> select (); / / use select instead of find here, 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']; / / get the ID of the think_tag table
$taggedids [] = $value ['id']; / / get the ID of the think_tagged table
}
/ / considering that a diary may have multiple tags, here is a traversal of $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 tags
If ($count==1) {/ / if $count==1, this tag is owned only by this diary. Delete it.
$tag- > where ('id='.$tagId)-> delete ()
} elseif ($count > 1) {/ / $count > 1, indicating that this tag is owned by multiple diaries and cannot be deleted, so subtract 1.
$tag- > where ('id='.$tagId)-> setDec (' count',1); / / setDec subtracts $count by 1, note how to use thinkphp3.1.
}
}
/ / the following is the relevant data that deletes the diary stored in the think_tagged table
Foreach ($taggedids as $taggedid_k = > $taggedid_v) {
$tagged- > where ('id='.$taggedid_v)-> delete ()
}
}
The function is written, how to use it? The method is simple.
Let's take a look at the function to delete the diary.
The copy code is as follows:
Public function delete () {
/ / Delete the 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 Examples\ Blog\ Lib\ Action\ PublicAction.class.php. The BlogAction.class.php class inherits its delete function, so we call the deltag ($recordId) function in delete (), as follows:
The copy code is as follows:
Public function delete () {
/ / Delete the 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 the case of deleting a single diary. Of course, if you want to delete a diary in batch, just traverse the ID of deleting blog and call deltag ($recordId) to OK.
Thank you for reading, the above is the content of "how to completely delete the tag in thinkphp3.1", after the study of this article, I believe you have a deeper understanding of how to completely delete the tag in thinkphp3.1, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.