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 a field indicating the deletion status in the database
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.
- Set soft deletion parameters in the model file
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;?//?表示未刪除狀態(tài)的值 }
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.
- Perform soft deletion operation
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);?//?根據(jù)id獲取用戶實體 $user->delete();?//?執(zhí)行軟刪除
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.
- Query soft-deleted data
If you need to query soft-deleted data, we can use the withTrashed method to query. For example:
//?查詢所有的用戶數(shù)據(jù)(包含已經(jīng)軟刪除的數(shù)據(jù)) $userList?=?User::withTrashed()->select(); foreach?($userList?as?$user)?{ ????if?($user->delete_time)?{?//?判斷是否已經(jīng)被軟刪除 ????????//?如果已經(jīng)被軟刪除,則進行相應(yīng)的處理 ????}?else?{ ????????//?如果未被軟刪除,則進行相應(yīng)的處理 ????} }
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.
- Restore soft-deleted data
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();?//?根據(jù)id獲取已經(jīng)被軟刪除的用戶實體 $user->restore();?//?執(zhí)行數(shù)據(jù)恢復(fù)
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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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