2011-10-12 54 views
1

在Symfony 2中,我生成一个Bundle用于将任何类型的文档存储到数据库中,但我需要BLOB列类型。如何在使用Symfony 2的Doctrine 2中添加BLOB类型

TNX到this question我添加的类别BlobType成学说DBAL,但使用了新列型我不得不改变

主义\ DBAL \类型\型号

[...] 

const BLOB = 'blob'; 

[...] 

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

主义\ 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'; 
} 

现在我没有时间为'漂亮的解决方案',但将来我想恢复Doctrine类,并能够将新的列类型分配到Symfony 2引导程序中。 我想我应该编辑我的app/bootstrap.php.cache,但我不知道如何介入。

回答

1

我刚刚发现这个要点: https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58

应用程序/ 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是自动生成的AFAIK。所以改变也就被覆盖。

+0

我知道了。实际上这是我添加的类...我认为bootstrap.php.cache只是第一次自我生成:还不太了解Symfony 2 ... – Ephraim

3

这个工作对我来说:

  1. 创建blobtype(见https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58

  2. 添加到您的包初始化(/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');   
        } 
    } 
    
2

小改进注册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'); 
     } 
} 
相关问题