Doctrine – 如何在测试环境中防止级联操作

huangapple go评论64阅读模式
英文:

Doctrine - how to prevent cascade action in testing environment

问题

我有这段代码:

class Event {
    /**
     * @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="events", cascade={"persist","remove"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     **/
    private $supplier;
    ...
}

当删除事件时,条件cascade={"persist","remove"}使得供应商也被删除。

我想仅在我的测试环境中更改此行为。

有没有什么技巧可以做到这一点?如果没有,我将不得不在每次删除事件时重新创建供应商....

英文:

I have this chunk of code :

class Event {
    /**
     * @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="events", cascade={"persist","remove"})
     * @ORM\JoinColumn(onDelete="SET NULL")
     **/
    private $supplier;
    ...
}

When an Event is deleted, the condition cascade={"persist","remove"} makes that the supplier is also deleted.

I would like to change this behavior only for my testing environment.

Is there a trick to do this ? if not, I will have to recreate the supplier each time an event is deleted....

答案1

得分: 1

我没有找到实现我想要的方式,但我唯一的解决方案是在删除事件之前将属性$supplier设置为null,这样就不会对任何供应商进行级联删除。

英文:

I did not find a way to do what I want, but the only solution I had was to set the property $supplier to null before deleting the event, so there's no cascade removing on any supplier.

huangapple
  • 本文由 发表于 2023年6月27日 18:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定