【问题标题】:How add BLOB type in Doctrine 2 using Symfony 2如何使用 Symfony 2 在 Doctrine 2 中添加 BLOB 类型
【发布时间】:2011-12-06 00:42:00
【问题描述】:

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

Tnx to this question 我将 BlobType 类添加到 Doctrine DBAL 中,但为了使用新的列类型,我必须更改

Doctrine\DBAL\Types\Type

[...]

const BLOB = 'blob';

[...]

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

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

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

【问题讨论】:

    标签: doctrine-orm blob symfony


    【解决方案1】:

    这对我有用:

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

    2. 将此添加到您的 Bundle 初始化 (/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】:

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

      【讨论】:

        【解决方案3】:

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

        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 是自动生成的 AFAIK.. 所以那里的更改会被覆盖。

        【讨论】:

        • 我知道。实际上是我添加的类...我以为bootstrap.php.cache是​​第一次自己生成的:还不太了解Symfony 2...
        猜你喜欢
        • 1970-01-01
        • 2012-01-08
        • 2013-07-02
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多