简体   繁体   English

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

[英]How add BLOB type in Doctrine 2 using Symfony 2

In Symfony 2 I generate a Bundle for storing any type of document into database, but I need the BLOB column type. 在Symfony 2中,我生成了一个Bundle,用于将任何类型的文档存储到数据库中,但是我需要BLOB列类型。

Tnx to this question I add the class BlobType into Doctrine DBAL, but for use the new column type I had to change 这个问题上,我将类BlobType添加到Doctrine DBAL中,但是要使用新的列类型,我必须进行更改

Doctrine\\DBAL\\Types\\Type 原则\\ DBAL \\类型\\类型

[...]

const BLOB = 'blob';

[...]

private static $_typesMap = array(
    [...],
    self::BLOB => 'Doctrine\DBAL\Types\BlobType',
);

Doctrine\\DBAL\\Platforms\\MySqlPlatform (maybe it was better if I had changed Doctrine\\DBAL\\Platforms\\AbstractPlatform) Doctrine \\ DBAL \\ Platforms \\ MySqlPlatform (如果我更改了Doctrine \\ DBAL \\ Platforms \\ AbstractPlatform,可能会更好)

[...]
protected function initializeDoctrineTypeMappings()
{
    $this->doctrineTypeMapping = array(
        [...],
        'blob'          => 'blob',
    );
}

[...]

/**
 * Obtain DBMS specific SQL to be used to create time fields in statements
 * like CREATE TABLE.
 *
 * @param array $fieldDeclaration
 * @return string
 */
public function getBlobTypeDeclarationSQL(array $fieldDeclaration) 
{
    return 'BLOB';
}   

Now I don't have mouch time for a 'pretty solution' , but in future I would like to restore the Doctrine classes and be able to assign the new column type into Symfony 2 bootstrap. 现在我没有时间来解决“漂亮的解决方案” ,但是将来我想恢复Doctrine类并能够将新的列类型分配给Symfony 2引导程序。 I think I should edit my app/bootstrap.php.cache but I don't have idea how to intervene. 我认为我应该编辑我的app / bootstrap.php.cache,但我不知道如何进行干预。

this worked for me: 这为我工作:

  1. create your blobtype (See https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58 ) 创建您的Blob类型(请参阅https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58

  2. add this to your Bundle initialization (/src/YOURDOMAIN/YOURBUNDLE/YOURDOMAINYOUBUNDLE.php) 将此添加到您的捆绑包初始化中(/src/YOURDOMAIN/YOURBUNDLE/YOURDOMAINYOUBUNDLE.php)

     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'); } } 

Small improvement for registration blob type in XXXBundle::boot() , but can be necessary during unittests. XXXBundle :: boot()中的注册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');
      }
}

I just found this gist: https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58 我刚刚发现了这个要点: https : //gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58

app/bootstrap.php: 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');

BTW bootstrap.cache.php is auto-generated AFAIK.. So changes there would be overwritten. BTW bootstrap.cache.php是自动生成的AFAIK。因此更改将被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM