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.
1 public function childid($priid){2 $data = $this->select();3 return $this->getchildid($data,$priid);4 }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 }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 }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!

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
