When developing web applications, we usually need to add, delete, modify and check data, and deleting data is one of the very important operations. However, in actual projects, we often need to recover deleted data, so traditional physical deletion operations are inconvenient.
In response to this problem, the ThinkPHP framework provides a soft deletion function, which converts the deletion operation into a marked deletion operation. In this way, the deleted data will not really disappear from the database, but will be added to the data table. A field indicating the status of deletion, which can be restored if necessary.
Next, let’s take a look at the soft deletion related implementation of the ThinkPHP framework.
1. How to implement soft deletion
Add it to the table that needs soft deletion A field indicating the deletion status, for example:
ALTER TABLE `table_name` ADD `delete_time` BIGINT(20) UNSIGNED DEFAULT NULL COMMENT '删除时间';
The delete_time field is used to record the time of the deletion operation. If this field is not empty, it means that the data has been deleted.
In the model file, we need to set the parameters of soft deletion, so that when we perform a deletion operation, This parameter will be updated automatically. For example:
namespace app\common\model; use think\Model; use traits\model\SoftDelete; class User extends Model { use SoftDelete; protected $deleteTime = 'delete_time'; // 表示删除时间的字段名称 protected $defaultSoftDelete = 0; // 表示未删除状态的值 }
Among them, the $deleteTime variable represents the field name of the deletion time, and the $defaultSoftDelete variable represents the value of the undeleted status. If this parameter is not set, it defaults to 0.
Where soft deletion is required, we can use the delete method provided by the model class to perform the deletion operation. For example:
$user = User::get($id); // 根据id获取用户实体 $user->delete(); // 执行软删除
After the soft delete operation is executed, the delete_time field will be updated to the current timestamp, indicating that the data has been deleted.
If you need to query soft-deleted data, we can use the withTrashed method to query. For example:
// 查询所有的用户数据(包含已经软删除的数据) $userList = User::withTrashed()->select(); foreach ($userList as $user) { if ($user->delete_time) { // 判断是否已经被软删除 // 如果已经被软删除,则进行相应的处理 } else { // 如果未被软删除,则进行相应的处理 } }
Through the withTrashed method, we can obtain all user data, including data that has not been soft deleted and data that has been soft deleted.
If you need to restore soft-deleted data, we can use the restore method provided by the model class to perform the restore operation. For example:
$user = User::onlyTrashed()->where('id', $id)->find(); // 根据id获取已经被软删除的用户实体 $user->restore(); // 执行数据恢复
After the soft deleted data recovery operation is executed, the value of the corresponding delete_time field will be cleared, indicating that the data has been recovered.
2. Summary
Through the soft deletion operation, we can delete the data while retaining the integrity of the data, and restore the deleted data when necessary. In the ThinkPHP framework, the implementation of soft deletion is very simple. You only need to add the corresponding fields in the database and set the soft deletion parameters of the model class. You can enjoy the convenience of the soft deletion function, improve development efficiency, and save development time.
The above is the detailed content of Implementation method of soft deletion in ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!