Disable Soft Deleteable filter for hard delete record doesn't work

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

I'm trying to disable "Soft Deleteable" filter during functional testing and I do it as follow:

First option: tried to disable at tearDown() in my test:

protected function tearDown() {     parent::tearDown();      $entityUser = $this->em->getRepository("UserSecurityBundle:User")->find($this->user->getUser()->getId());      $filter = $this->em->getFilters()->disable('softdeleteable');      $this->em->remove($entityUser);     $this->em->flush();     $this->em->close(); } 

Didn't work.

Second option: make a doctrine_test.yml and import in config_test.yml:

imports:     - { resource: config.yml }     - { resource: doctrine_test.yml }     - { resource: security_test.yml } 

In this case I remove the doctrine.yml and include in config_prod.yml.

Didn't work.

This is how my doctrine_test.yml section look like:

filters:     softdeleteable:         class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter         enabled: false 

Third option: disable the filter in setUp():

public function setUp() {     static::$kernel = static::createKernel();     static::$kernel->boot();     $this->em = static::$kernel->getContainer()->get('doctrine')->getManager();      $fixture = new LoadFeeData();     $fixture->load($this->em);      $this->em->getFilters()->disable('softdeleteable');     $this->user = $this->createUser();      parent::setUp(); } 

Didn't work.

Any advice? Solution?

回答1:

So after some research, after doing more test I found the solution, see below:

protected function tearDown() {     parent::tearDown();      $entityAccount = $this->em->getRepository("UserSecurityBundle:Account")->find(array("id" => $this->user->getId(), "user" => $this->user->getUser()->getId()));     $entityUser = $entityAccount->getUser();      $entityCompanyHasWtax = $this->em->getRepository("ApprovalBundle:CompanyHasWtax")->findOneBy(array("company" => $this->company, "wtax" => $this->fee, "user" => $this->user->getUser()->getId()));      foreach ($this->em->getEventManager()->getListeners() as $eventName => $listeners) {         foreach ($listeners as $listener) {             if ($listener instanceof \Gedmo\SoftDeleteable\SoftDeleteableListener) {                 $this->em->getEventManager()->removeEventListener($eventName, $listener);             }         }     }      $this->em->remove($entityCompanyHasWtax);     $this->em->remove($entityAccount);     $this->em->remove($entityUser);      $this->em->flush();     $this->em->close(); } 

Aparently Doctrine has a bug since disabling the filter in this way:

$this->em->getFilters()->disable('softdeleteable'); 

Doesn't work, good look for others



回答2:

Although this question is a bit old maybe it is useful to someone:

Creating your own event listener might be a better solution:

class SoftDeleteableListener extends BaseSoftDeleteableListener {  /**  * @inheritdoc  */ public function onFlush(EventArgs $args) {     $ea = $this->getEventAdapter($args);     $om = $ea->getObjectManager();     //return from event listener if you disabled filter: $em->getFilters()->disable('softdeleteable');     if (!$om->getFilters()->isEnabled('softdeleteable')) {         return;     }      parent::onFlush($args); }  } 

And adding in your config:

gedmo.listener.softdeleteable:     class: AppBundle\EventListener\SoftDeleteableListener     tags:         - { name: doctrine.event_subscriber, connection: default }     calls:         - [ setAnnotationReader, [ @annotation_reader ] ] 

source: https://github.com/Atlantic18/DoctrineExtensions/issues/1175



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!