【问题标题】:Symfony 2: Display an image mediumblobSymfony 2:显示图像 mediumblob
【发布时间】: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


【解决方案1】:

是的,我在控制器文件中添加了这样的类型:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 2013-11-02
  • 1970-01-01
  • 2012-10-30
  • 2020-02-03
相关资源
最近更新 更多