Soft delete best practices (PHP/MySQL)

前端 未结 6 2102
予麋鹿
予麋鹿 2020-12-02 18:32

Problem

In a web application dealing with products and orders, I want to maintain information and relationships between former employees (users) and

6条回答
  •  青春惊慌失措
    2020-12-02 19:30

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

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

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

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

提交回复
热议问题