How add BLOB type in Doctrine 2 using Symfony 2

跟風遠走 提交于 2019-11-28 13:09:07

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!