Reputation: 1443
I'm trying to display a MEDIUMBLOB field of my database, which contain images, with Symfony 2 but I can't do it...
To do this, I created my own Doctrine type. Here is the code:
<?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);
}
}
Then, to display the image I'm doing this:
<img src="data:image/jpeg;base64,{{ photo }}">
where the photo variable is defined in my controller like this:
$photo = base64_encode($user->getPhoto());
But nothing is displayed
Thanks for your help !
Upvotes: 1
Views: 718
Reputation: 1443
Yes, I added my type like that in my controller file:
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');
}
Upvotes: 1