KnpLabs 使用 SoftDeletable 时强制删除原则实体 [英] Force delete doctrine entity when using SoftDeletable by KnpLabs
本文介绍了KnpLabs 使用 SoftDeletable 时强制删除原则实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在来自 https://github.com/KnpLabs/DoctrineBehaviors/的实体中使用 SoftDeletable 特征#softDeletable 它工作正常,但有时我想强制删除实体.我该怎么做?
I'm using SoftDeletable trait in entities from https://github.com/KnpLabs/DoctrineBehaviors/#softDeletable It's working fine, but sometimes I'd like to force delete the entity. How can I do that?
当我使用 $em->remove($entity)
时,它会被软删除,但我需要将它从数据库中完全删除.
When I use $em->remove($entity)
, it gets soft-deleted but I need to remove it completely from the database.
推荐答案
只需从 EventManager
中删除订阅者并在 remove()
/ 之后重新添加它>flush()
操作.
Just remove the subscriber from the EventManager
and add it back after the remove()
/ flush()
operation.
// get the event-manager
$eventManager = $this->get('doctrine')->getEventManager();
// get the listener
$subscriber = $this->get('knp.doctrine_behaviors.softdeletable_subscriber');
// remove the the subscriber for all events
$eventManager->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
// remove the entity
$em->remove($entity);
$em->flush();
// add it back to the event-manager
$eventManager->addEventSubscriber($subscriber);
这篇关于KnpLabs 使用 SoftDeletable 时强制删除原则实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文