1

这就是我的捆绑主文件的方式

<?php

namespace Foo\FooBundle;

use Doctrine\DBAL\Types\Type;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class FooBundle extends Bundle
{
    public function build(ContainerBuilder $container) 
    {
        parent::build($container);
    }

    public function boot()
    {
        $em = $this->container->get('doctrine.orm.entity_manager');
        Type::addType('custom_date', 'Foo\FooBundle\Doctrine\DBAL\Types\CustomDate');
        $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_date','custom_date');
     }
}

所以你可能晚上我有一个自定义类型,我在boot()我的包操作期间注册了它。

现在,在我的测试中,我遇到了一些问题。一些测试需要使用静态函数setUpBeforeClass()tearDownAfterClass()因为我需要EntityManagerContainer. 在我的功能下方(对多个测试类通用)

public static function setUpBeforeClass()
{
    static::$kernel = static::createKernel();
    static::$kernel->boot();
    self::$em = static::$kernel->getContainer()
        ->get('doctrine')
        ->getManager()
    ;
}

public static function tearDownAfterClass()
{
    parent::tearDown();
    self::$em->close();
}

现在,如果我运行多个使用static::$kernel->boot();某些测试的测试类,则由于 DBAL 异常声称该custom_date类型已注册而失败。

这对我来说很奇怪,因为parent::tearDown();功能是

/**
 * Shuts the kernel down if it was used in the test.
 */
protected function tearDown()
{
    if (null !== static::$kernel) {
        static::$kernel->shutdown();
    }
}

并且static::$kernel->shutdown();

public function shutdown()
{
    if (false === $this->booted) {
        return;
    }

    $this->booted = false;

    foreach ($this->getBundles() as $bundle) {
        $bundle->shutdown();
        $bundle->setContainer(null);
    }

    $this->container = null;
}

所以,对我来说,如果 bundle 被关闭了,那么即使是他的自定义类型也应该被取消注册。似乎他们没有。


我的解决方案

我修改boot()了函数以检查类型是否已注册

public function boot()
{
    if (false === Type::hasType('custom_date')) {
        $em = $this->container->get('doctrine.orm.entity_manager');
        Type::addType('custom_date', 'Foo\FooBundle\Doctrine\DBAL\Types\CustomDate');
        $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_date','custom_date');
}

}


问题)

  1. 有谁知道为什么当捆绑被喊下来时 DBAL 类型没有被取消注册?
  2. 我的解决方案安全吗?是唯一的解决方案还是您认识其他人?
4

0 回答 0