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

Soft delete best practices (PHP/MySQL)(软删除最佳实践(PHP/MySQL))
本文介绍了软删除最佳实践(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死锁异常代码?)
PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滚动游标不起作用)
PHP PDO ODBC connection(PHP PDO ODBC 连接)
Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔术方法)
php pdo get only one value from mysql; value that equals to variable(php pdo 只从 mysql 获取一个值;等于变量的值)
MSSQL PDO could not find driver(MSSQL PDO 找不到驱动程序)