软删除最佳实践(PHP / MySQL) [英] Soft delete best practices (PHP/MySQL)
问题描述
问题
在处理产品和订单的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 Deleted1
-> Soft Deleted, shows up in lists of deleted items for management users2
-> Soft Deleted, does not show up for any user except admin users3
-> 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!