软删除最佳实践(PHP/MySQL)

2023-05-07php开发问题
11

本文介绍了软删除最佳实践(PHP/MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

问题

在处理产品和订单的 Web 应用程序中,我想维护前雇员(用户)与他们处理的订单之间的信息和关系.我想维护过时产品和包含这些产品的订单之间的信息和关系.

In a web application dealing with products and orders, I want to maintain information and relationships between former employees (users) and the orders they handled. I want to maintain information and relationships between obsolete products and orders which include these products.

但是我希望员工能够清理管理界面,例如删除前员工、过时的产品、过时的产品组等.

However I want employees to be able to de-clutter the administration interfaces, such as removing former employees, obsolete products, obsolete product groups etc.

我正在考虑实施软删除.那么,人们通常如何做到这一点?

I'm thinking of implementing soft-deletion. So, how does one usually do this?

我的即时想法

我的第一个想法是在每个应该软删除的对象表中粘贴一个flag_softdeleted TINYINT NOT NULL DEFAULT 0"列.或者可以改用时间戳?

My first thought is to stick a "flag_softdeleted TINYINT NOT NULL DEFAULT 0" column in every table of objects that should be soft deletable. Or maybe use a timestamp instead?

然后,我在每个相关的 GUI 中提供一个显示已删除"或取消删除"按钮.单击此按钮,您将在结果中包含软删除的记录.每个删除的记录都有一个恢复"按钮.这有意义吗?

Then, I provide a "Show deleted" or "Undelete" button in each relevant GUI. Clicking this button you will include soft-deleted records in the result. Each deleted record has a "Restore" button. Does this make sense?

你的想法?

此外,如果您提供任何相关资源的链接,我将不胜感激.

Also, I'd appreciate any links to relevant resources.

推荐答案

我就是这样做的.我有一个 is_deleted 字段,默认为 0.然后查询只需检查 WHERE is_deleted = 0.

That's how I do it. I have a is_deleted field which defaults to 0. Then queries just check WHERE is_deleted = 0.

我尽量远离任何硬删除.有时它们是必要的,但我将其设为仅限管理员的功能.这样我们可以硬删除,但用户不能...

I try to stay away from any hard-deletes as much as possible. They are necessary sometimes, but I make that an admin-only feature. That way we can hard-delete, but users can't...

实际上,您可以使用它在您的应用程序中使用多个层"软删除.所以每个都可以是一个代码:

In fact, you could use this to have multiple "layers" of soft-deletion in your app. So each could be a code:

  • 0 -> 未删除
  • 1 -> 软删除,显示在管理用户的已删除项目列表中
  • 2 -> 软删除,除管理员用户外不显示任何用户
  • 3 -> 仅对开发者显示.
  • 0 -> Not Deleted
  • 1 -> Soft Deleted, shows up in lists of deleted items for management users
  • 2 -> Soft Deleted, does not show up for any user except admin users
  • 3 -> Only shows up for developers.

拥有其他 2 个级别仍然允许经理和管理员在删除列表太长时清理它们.而且由于前端代码只检查 is_deleted = 0,它对前端是透明的...

Having the other 2 levels will still allow managers and admins to clean up the deleted lists if they get too long. And since the front-end code just checks for is_deleted = 0, it's transparent to the frontend...

这篇关于软删除最佳实践(PHP/MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17