问题
I know you can soft delete in Doctrine (i.e. do not delete a record but rather add a "deleted" value). There's an extension for that.
Now I wonder if there's a way to "soft update" a record. I mean not actually update the record but rather create a new record and make the old one invalid. In the same extension as soft-delete, there's a function loggable
, but this one logs to a different table.
I could create a controller that, instead of updating, soft-deletes (and thus invalidates) the old record, and then creates a new one with the new values. But I'm unsure if this is a good practice.
Maybe I should create this action on the object itself? But I'm unsure how to do this.
Edit
I've looked into Versionable and EntityAudit (as suggested by Tomas), but it seems these bundles do way too much. I merely want to check if a given field is different from the old one, and if not: soft-delete the old one (I'm using softDeleteable so a simple remove()
will do); then create a new one with the changed values.
So ideally it would lurk in the shadows until an update is performed. Then read from the mapping configuration which fields it needs to watch, and if these fields are indeed different from what's persisted, the program should execute the remove()
and persist()
commands.
回答1:
This extension might suit your use case:
- simplethings/EntityAudit
It records any changes you want to track. So it should be pretty easy to modify it to meed your needs.
来源:https://stackoverflow.com/questions/38384988/symfony-doctrine-soft-update-if-a-certain-field-has-changed