我试图用Symfony 2显示我的数据库的MEDIUMBLOB字段,其中包含图像,但我无法做到......
为此,我创建了自己的Doctrine类型。这是代码:
<?php
/**
* Personal type to support MediumBlob
*
* @author Leglopin
*/
namespace MC\UserBundle\Doctrine\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Types\Type;
/**
* Type that maps a PHP object to a mediumblob SQL type.
*/
class MediumBlobType extends Type
{
const MEDIUMBLOB = 'mediumblob';
public function getName()
{
return self::MEDIUMBLOB;
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDoctrineTypeMapping('MEDIUMBLOB');
}
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);
}
}
然后,要显示我执行此操作的图像:
<img src="data:image/jpeg;base64,{{ photo }}">
照片变量在我的控制器中定义如下:
$photo = base64_encode($user->getPhoto());
但没有显示任何内容
感谢您的帮助!
答案 0 :(得分:0)
是的,我在我的控制器文件中添加了我的类型:
public function boot()
{
$em = $this->container->get('doctrine.orm.default_entity_manager');
Type::addType('mediumblob', 'MC\UserBundle\Doctrine\Types\MediumBlobType');
$em->getConnection()->getDatabasePlatform()
->registerDoctrineTypeMapping('MEDIUMBLOB', 'mediumblob');
}