软删除最佳实践(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...
相关文章