禁用软删除过滤器用于硬删除记录不起作用 [英] Disable Soft Deleteable filter for hard delete record doesn't work

查看:124
本文介绍了禁用软删除过滤器用于硬删除记录不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在功能测试期间禁用软删除过滤器,我做如下:



第一个选项:尝试在 tearDown()在我的测试中:

  protected function tearDown(){
父:: 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();
}

未奏效。



第二个选项:make一个doctrine_test.yml并在config_test.yml中导入:

  
- {resource:config.yml}
- {resource:doctrine_test.yml}
- {resource:security_test.yml}
/ pre>

在这种情况下,我删除 doctrine.yml 并包含在 config_prod中。 yml



未奏效。



这是我的 doctrine_test.yml 部分的样子如下:

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

第三个选项:在 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();
}

未奏效。



任何建议?解决方案?

解决方案

经过一番研究,做了更多的测试后,我找到了解决方案,见下文:

  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->费用,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();
}

由于禁用过滤器的方式,原则上有一个错误:

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

不行,好找别人


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?

解决方案

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

这篇关于禁用软删除过滤器用于硬删除记录不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆