Doctrine, Symfony And Blob

This is a simple help yo add mysql blob type to Doctrine and Symfony 2:

  1. Create BlobType.php file in Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/

    namespace Doctrine\DBAL\Types;
    
    use Doctrine\DBAL\Platforms\AbstractPlatform;
    
    class BlobType extends Type
    {
        const BLOB = 'blob';
    
        public function getName()
        {
            return self::BLOB;
        }
    
        public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
        {
            return $platform->getDoctrineTypeMapping('BLOB');
        }
    
        public function convertToDatabaseValue($value, AbstractPlatform $platform)
        {
            return ($value === null) ? null : base64_encode($value);
        }
    
        public function convertToPHPValue($value, AbstractPlatform $platform)
        {
            return ($value === null) ? null : base64_decode($value);
        }
    }
    ?>
    

  2. In Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/Type.php add

             const BLOB = 'blob';
             self::BLOB => 'Doctrine\DBAL\Types\BlobType', to $_typesMap array;
             

  3. Add this method to your class in DomainYourBundle.php in Symfony/src/YourDomain/YourBundle/

        public function boot()
        {
            $em = $this->container->get('doctrine.orm.entity_manager');
            if (!Type::hasType('blob'))
            {
                Type::addType('blob', 'Doctrine\DBAL\Types\Type\BlobType');
                $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');
            }
        }