Doctrine: on persist: soft-delete only if object has changed

别等时光非礼了梦想. 提交于 2019-12-12 01:07:48

问题


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.



来源:https://stackoverflow.com/questions/38423582/doctrine-on-persist-soft-delete-only-if-object-has-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!