使用KnpLabs使用SoftDeletable时强制删除原则实体 [英] Force delete doctrine entity when using SoftDeletable by KnpLabs
本文介绍了使用KnpLabs使用SoftDeletable时强制删除原则实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 https://github.com/KnpLabs/DoctrineBehaviors/的实体中使用SoftDeletable trait #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屋!
查看全文