Skip to content

Instantly share code, notes, and snippets.

@romansklenar
Created August 15, 2010 03:36
Show Gist options
  • Save romansklenar/525030 to your computer and use it in GitHub Desktop.
Save romansklenar/525030 to your computer and use it in GitHub Desktop.
Doctrine 2, BLOB datatype implementation
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Type that maps a PHP binary data to a blob SQL type.
*
* @since 2.0
* @author Roman Sklenar <[email protected]>
*/
class Blob extends Type
{
const BLOB = 'blog';
public function getName()
{
return self::BLOB;
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDoctrineTypeMapping('BLOB');
}
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);
}
}
<?php
// ...
$em = Doctrine\ORM\EntityManager::create($conn, $config, $evm);
// types registration
Doctrine\DBAL\Types\Type::addType('blob', 'Doctrine\DBAL\Types\Blob');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('BLOB', 'blob');
<?php
namespace Entities;
/**
* @Entity
* @Table(name="Photos")
*/
class Photo extends Base
{
/** @Column(type="integer") @Id @GeneratedValue */
protected $id;
// ...
/** @Column(type="blob") */
protected $data;
/**
* Converts instance to Nette\Image.
*
* @return Image
*/
public function toImage() {
return Nette\Image::fromString($this->data);
}
}
@zebba
Copy link

zebba commented Dec 13, 2012

We have had problems using this implementation when we tried to duplicate entries in the database by copying/cloning. The operation always resulted in a 0-sized entry in the blob field.

Once you add an is_file()-check and decide from there it was working however.

@egor-xyz
Copy link

How I can get value from parameters.yml in Class *** extended Type? I need to take public key for RSA encrypt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment