1

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 !

1
  • Do you added the type? Take a look here Commented Aug 6, 2014 at 4:57

1 Answer 1

1

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');
}
Sign up to request clarification or add additional context in comments.

1 Comment

The question and answer helped me implement MEDIUM blob in Laravel! Thanks! this Medium article too: medium.com/@matriphe/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.