【发布时间】:2014-08-05 21:20:33
【问题描述】:
我正在尝试使用 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());
但是什么都没有显示
感谢您的帮助!
【问题讨论】:
-
你添加了类型吗?看看here
标签: php mysql symfony doctrine