search
HomePHP FrameworkThinkPHPImplementation method of soft deletion in ThinkPHP framework

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

  1. 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.

  1. 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; // 表示未删除状态的值
}

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.

  1. 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); // 根据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.

  1. Query soft-deleted data

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.

  1. 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(); // 根据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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software