Doctrine:on persist:仅当对象发生变化时,才能进行软删除 [英] Doctrine: on persist: soft-delete only if object has changed

查看:93
本文介绍了Doctrine:on persist:仅当对象发生变化时,才能进行软删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教条/ Symfony3中,如何才能最好检查记录是否要更改,以便删除/创建新的记录,而不是更新?



上下文:每当供应商发送一个数据库时,我将大量的价格表csv加载到数据库中。重要的是,更新的价格不会反映在以前订单中。当然,我可以在每次列表上传时删除所有对象,并创建新的对象,但会导致数据库不必要的大量,并且需要不必要的调用。



因此我想要软件删除一个对象,只有当它被更改时才创建一个对象。

解决方案

作为:




  • 如果实体已更改

  • 软删除

  • 使用新值创建新记录



您需要2个步骤才能执行



1)使用 https://github.com/simplethings/EntityAudit 创建新对象更改



2)添加侦听 prePersist (或 preFlush / preUpdate ;选择最适合您的一个),并使用与上述包中类似的过程(寻求 xxxSubscriber / xxxListener )软删除它。


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.

解决方案

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.

这篇关于Doctrine:on persist:仅当对象发生变化时,才能进行软删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆