Doctrine orm 如何使用Symfony 2在条令2中添加BLOB类型

Doctrine orm 如何使用Symfony 2在条令2中添加BLOB类型,doctrine-orm,blob,symfony,Doctrine Orm,Blob,Symfony,在symfony2中,我生成了一个包,用于将任何类型的文档存储到数据库中,但我需要BLOB列类型 我将类BlobType添加到Doctrine DBAL中,但为了使用新的列类型,我不得不更改 条令\DBAL\Types\Type [...] const BLOB = 'blob'; [...] private static $_typesMap = array( [...], self::BLOB => 'Doctrine\DBAL\Types\BlobType',

在symfony2中,我生成了一个包,用于将任何类型的文档存储到数据库中,但我需要BLOB列类型

我将类BlobType添加到Doctrine DBAL中,但为了使用新的列类型,我不得不更改

条令\DBAL\Types\Type

[...]

const BLOB = 'blob';

[...]

private static $_typesMap = array(
    [...],
    self::BLOB => 'Doctrine\DBAL\Types\BlobType',
);
条令\DBAL\Platforms\MySqlPlatform(如果我更改了条令\DBAL\Platforms\AbstractPlatform,可能会更好)

现在我没有足够的时间去寻找一个“漂亮的解决方案”,但在将来,我想恢复条令类,并能够将新的列类型分配到symfony2引导程序中。 我想我应该编辑我的app/bootstrap.php.cache,但我不知道如何进行干预。

我刚刚发现了以下要点:

app/bootstrap.php:

<?php

// ...
$em = Doctrine\ORM\EntityManager::create($conn, $config, $evm);

// types registration
Doctrine\DBAL\Types\Type::addType('blob', 'Doctrine\DBAL\Types\Blob');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('BLOB', 'blob');
这对我很有用:

  • 创建blobtype(请参见)

  • 将其添加到Bundle初始化中(/src/YOURDOMAIN/YOURBUNDLE/YOURDOMAINYOUBUNDLE.php)


  • 注册的小改进blob输入XXXBundle::boot(),但在单元测试期间可能是必需的

    class XXXBundle extends Bundle
    {
       public function boot()
       {
          // Add blob type
          if(!Type::hasType('blob')) {
             Type::addType('blob', '{CLASS_PATH}\\Blob');
          }
    
          // Add blob type to current connection.
          // Notice: during tests there can be multiple connections to db so 
          // it will be needed to add 'blob' to all new connections if not defined. 
          $em = $this->container->get('doctrine.orm.entity_manager');
          if (!$em->getConnection()->getDatabasePlatform()->hasDoctrineTypeMappingFor('blob')) {
               $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');
          }
    }
    

    我知道。事实上,这是我添加的类。。。我以为bootstrap.php.cache只是第一次自生成的:还不太了解Symfony 2。。。
    class YourBundle extends Bundle
    {
        public function boot()
        {
            $em = $this->container->get('doctrine.orm.entity_manager');
            Type::addType('blob', 'YOURDOMAIN\YOURBUNDLE\YOURTYPEDIRECTORY\BlobType');
            $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');        
        }
    }
    
    class XXXBundle extends Bundle
    {
       public function boot()
       {
          // Add blob type
          if(!Type::hasType('blob')) {
             Type::addType('blob', '{CLASS_PATH}\\Blob');
          }
    
          // Add blob type to current connection.
          // Notice: during tests there can be multiple connections to db so 
          // it will be needed to add 'blob' to all new connections if not defined. 
          $em = $this->container->get('doctrine.orm.entity_manager');
          if (!$em->getConnection()->getDatabasePlatform()->hasDoctrineTypeMappingFor('blob')) {
               $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');
          }
    }