ThinkPHP deletes a column (single) and thinkphp deletes a column
When we are working on some website projects, we will encounter such a problem: deleting a topic is not the underlying topic. That is to say, the deleted topic has a subtopic, when we execute the command to delete this topic, we need to delete this topic and its subtopics together, because we cannot allow the parent topic of a class to be deleted, the subtopic is also displayed or in the database. At this time, the topic deletion problem arises.
First:
This is a simple permission management page. In the figure, the Administrator permission is the top-level permission, the topic management is the sub-permission of the Administrator permission, and the topic is added as the sub-permission of the topic management, the effect we want to achieve is: Delete the Administrator permission, and all the topic management and topic addition permissions under it are deleted at the same time; Delete the topic management permissions, the topic addition is also deleted; to delete a topic, you only need to delete it.
The following implementation method:
1. simple deletion method in the controller:
1 public function privilege_del () {2 $ pri = D ('privilege'); 3 $ id = I ('id '); 4 if ($ pri-> delete ($ id) {5 $ this-> success ('permission deleted successfully! ', U ('privilege/privilege_lst'); 6} else {7 $ this-> error ('failed to delete the permission! '); 8} 9}
2. we think about the results we want to delete at the same time. In fact, when we choose to delete a parent column, the code first helps us Delete the bottom-level sub-columns of this column class, and then deletes them in turn, finally, we will delete the selected parent column, so we need to write a pre-constructor to operate _ before_delete ($ options). This is the function provided by ThinkPHP, its usage is to execute this function before we execute the delete method. $ options indicates the information we want to delete, which is a two-dimensional array. When we dump $ options dump out, result:
That is to say, if we want to obtain the id value of this information, we only need to obtain $ options ['where'] ['id'], but note that, if the where clause in the array is also an array, this means that we perform batch deletion, which is also the difference between single deletion and batch deletion, in the future, I will write another article to give a brief introduction. This time we will only write a single Delete.
First, write a childid method to get all the data. $ priid here is the id of the column to be deleted
1 public function childid($priid){2 $data = $this->select();3 return $this->getchildid($data,$priid);4 }
Then, based on all the data, find the id of the subtopic under the topic to be deleted, and create a method again, getchildid, to obtain all the IDs to be deleted.
1 public function getchildid($data,$parentid){ 2 static $ret=array(); 3 foreach ($data as $k => $v) { 4 if($v['parentid']==$parentid){ 5 $ret[]=$v['id']; 6 $this->getchildid($data,$v['id']); 7 } 8 } 9 return $ret;10 }
Recursion is used here. After querying the lower-level columns in all data, query the lower-level columns of the lower-level columns, and save the id of all the sub-sections of the column to the ret array,.
Finally, call it in the constructor.
1 public function _ before_delete ($ options) {2 // delete a single 3 $ chilrenids = $ this-> childid ($ options ['where'] ['id']); 4 $ chilrenids = implode (',', $ chilrenids); 5 if ($ chilrenids) {6 $ this-> execute ("delete from ed_privilege where id in ($ chilrenids) "); 7} 8}
Separate the obtained IDs with commas (,), because the delete method cannot be used to delete data. If yes, the constructor will be called again to form an endless loop, therefore, we need to execute SQL statements here to delete data.
The above is all the content deleted from a single topic.