简体   繁体   中英

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.

Tnx to this question I add the class BlobType into Doctrine DBAL, but for use the new column type I had to change

Doctrine\\DBAL\\Types\\Type

[...]

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)

[...]
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. I think I should edit my app/bootstrap.php.cache but I don't have idea how to intervene.

this worked for me:

  1. create your blobtype (See https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58 )

  2. add this to your Bundle initialization (/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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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