2011-10-12 51 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'); 
     } 
} 
相關問題