可删除)在symfony中过滤原则

为Doctrine 2安装和使用SoftDeleteable行为扩展非常简单。 问题通常是试图禁用某些代码部分并再次启用它。 您可能想要这样做:

  • 加载被软删除的实体
  • 完全绕过软删除过滤器从数据库中删除实体
  • 那么如何禁用它呢?


    1.如何加载软删除的实体

    根据文档,禁用实体管理器的过滤器:

    $em->getFilters()->disable('softdeleteable');
    $object = $em->find('AppBundle:Object', 1); // soft-deleted entity will be loaded
    

    要再次启用软删除:

    $em->getFilters()->enable('softdeleteable');
    

    注意: $em->clear(); 可能需要在此行之前,如果实体已经加载了禁用的软删除过滤器。

    2.如何从数据库中完全删除实体

    尽管在文档中没有提及它,但如果您需要删除实体并绕过软删除筛选器,则第一种解决方案不起作用。 过滤器需要从实体管理器的事件侦听器中删除:

    // initiate an array for the removed listeners
    $originalEventListeners = [];
    
    // cycle through all registered event listeners
    foreach ($em->getEventManager()->getListeners() as $eventName => $listeners) {
        foreach ($listeners as $listener) {
            if ($listener instanceof GedmoSoftDeleteableSoftDeleteableListener) {
    
                // store the event listener, that gets removed
                $originalEventListeners[$eventName] = $listener;
    
                // remove the SoftDeletableSubscriber event listener
                $em->getEventManager()->removeEventListener($eventName, $listener);
            }
        }
    }
    
    // remove the entity
    $em->remove($object);
    $em->flush($object); // or $em->flush();
    
    // re-add the removed listener back to the event-manager
    foreach ($originalEventListeners as $eventName => $listener) {
        $em->getEventManager()->addEventListener($eventName, $listener);
    }
    

    参考文献:

  • https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md
  • KnpLabs使用SoftDeletable时强制删除原则实体
  • 禁用软删除过滤器的硬删除记录不起作用

  • 您可以使用服务来禁用和重新启用软删除过滤器行为:

    <?php
    
    namespace AppUtil;
    
    use DoctrineORMEntityManagerInterface;
    use GedmoSoftDeleteableSoftDeleteableListener;
    
    class SoftDeleteFilter
    {
        /**
         * @var string
         */
        const EVENT_NAME = 'onFlush';
    
        /**
         * @var object
         */
        private $originalEventListener;
    
        /**
         * @param EntityManagerInterface $em
         */
        public function removeSoftDeleteFilter(EntityManagerInterface $em)
        {
            foreach ($em->getEventManager()->getListeners() as $eventName => $listeners) {
                foreach ($listeners as $listener) {
                    if ($listener instanceof SoftDeleteableListener) {
                        if ($eventName === self::EVENT_NAME) {
                            $this->originalEventListener = $listener;
                            $em->getEventManager()->removeEventListener($eventName, $listener);
                        }
                    }
                }
            }
        }
    
        /**
         * @param EntityManagerInterface $em
         */
        public function undoRemoveSoftDeleteFilter(EntityManagerInterface $em)
        {
            if (empty($this->originalEventListener)) {
                throw new Exception('can not undo remove, soft delete listener was not removed');
            }
            // re-add the removed listener back to the event-manager
            $em->getEventManager()->addEventListener(self::EVENT_NAME, $this->originalEventListener);
        }
    }
    

    用法:

    $this->softDeleteFilter->removeSoftDeleteFilter($this->entityManager);
    $this->entityManager->remove($issue);
    $this->entityManager->flush();
    $this->softDeleteFilter->undoRemoveSoftDeleteFilter($this->entityManager);
    
    链接地址: http://www.djcxy.com/p/65035.html

    上一篇: deleteable) filter for doctrine in symfony

    下一篇: How to disable a doctrine filter in a param converter