Doctrine: on persist: soft-delete only if object h

2019-09-16 06:27发布

问题:

In Doctrine/Symfony3, how would I best check whether a record is about to be changed in order to delete/create a new one instead of updating?

Context: I'm loading a large price list csv into the database every time the supplier sends one. It is important that updated prices are not reflected in previously placed orders. Of course I could soft-delete all objects every time the list is uploaded, and create new ones, but that would make the database unnecessarily large and require unneccessary calls.

Therefore I want to soft-delete an object and create a new one only when it has been changed.

回答1:

I understand your requested workflow as:

  • if entity has changed
  • soft delete it
  • create new record with new values

You need 2 steps to do that

1) use https://github.com/simplethings/EntityAudit to create new object on change

2) add EventSubscriber that listens to prePersist (or preFlush/preUpdate; pick the one that suits you best), and use similar process as in package above (seek for xxxSubscriber/xxxListener) to soft delete it.