How add BLOB type in Doctrine 2 using Symfony 2

13,566

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
13,566
Ephraim
Author by

Ephraim

Updated on June 04, 2022

Comments

  • Ephraim
    Ephraim almost 2 years

    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.

  • Ephraim
    Ephraim over 12 years
    I know it. In fact it is the class that I added... I thought bootstrap.php.cache was self-generated only the first time: do not yet know very well Symfony 2...