How to delete a column?

PHP中文网
Release: 2023-03-10 20:42:01
Original
1893 people have browsed it

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     }
Copy after login

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 delete

1 public function childid($priid){2         $data = $this->select();3         return $this->getchildid($data,$priid);4     }
Copy after login

Then based on all Data search for the id of the sub-column below the column we want to delete, and create a method getchildid again to get all the ids that need 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     }
Copy after login

Here again recursion is used to query in all data After completing the lower-level columns, query the lower-level columns of the lower-level columns, and finally save the id versions of all sub-columns of the column into the ret array and return.
Finally call

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     }
Copy after login

in the constructor to separate the obtained ID numbers with commas, because the delete method can no longer be used to delete data at this time. If used, then Calling the constructor again will become an infinite loop, so we need to execute SQL statements here to delete data.
The above is all the content deleted from a single column.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!