When we do some website projects, we will encounter such a problem. Delete a column, and this column is not the bottom column. That is to say, the deleted column has sub-columns. At this time, we execute To delete the column command, you need to delete the column and its sub-columns together, because we cannot let the sub-column still be displayed or exist in the database after the superior column of a class is deleted. At this time, the problem of column deletion becomes coming.
First picture:
This is a simple permission management page. In the picture, the administrator permissions are top-level permissions, and the column management is administrator permissions. Sub-permissions of column addition are sub-permissions of column management. For such a three-level classification, the effect we want to achieve is: delete the administrator permission, and all the column management and column adding permissions under it will be deleted at the same time; delete the column management permission , the column addition will also be deleted at the same time; deleting the column addition permission will only delete the column itself.
Next implementation method:
1. It is a 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('删除权限成功!',U('Privilege/privilege_lst'));6 }else{7 $this->error('删除权限失败!');8 }9 }
2. We think that the result of simultaneous deletion we want is actually that when we choose to delete the upper-level column, the code first helps us delete the bottom-level sub-column of the column category, and then deletes it upwards in order, and finally deletes what we selected. The superior column, so we need to write a pre-constructor to operate_before_delete($options) This is the function provided to us by ThinkPHP. Its usage is to execute this function before we execute the delete method, where $options is The information we want to delete is specifically a two-dimensional array. When we dump $options, the result is: , you only need to get $options['where']['id'], but note that if where in the array is also an array, this means that we are performing batch deletion, which is also a single deletion and The difference between batch deletion and batch deletion. Regarding batch deletion, I will write another article to briefly introduce it. This time we will only write about single deletion.
First, write a method childid to get all the data. The $priid here is the id of the column we want to delete1 public function childid($priid){2 $data = $this->select();3 return $this->getchildid($data,$priid);4 }
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 }
1 public function _before_delete($options){2 //单个删除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 }
The above is the detailed content of How to delete a column?. For more information, please follow other related articles on the PHP Chinese website!