Skip to content

Instantly share code, notes, and snippets.

@ricbra
Forked from Sitebase/Doctrine.php
Last active August 29, 2015 14:23
Show Gist options
  • Save ricbra/eb96fb64035f5d2ffbae to your computer and use it in GitHub Desktop.
Save ricbra/eb96fb64035f5d2ffbae to your computer and use it in GitHub Desktop.
\Doctrine\DBAL\Types\Type::addType('uuid', 'BuboBox\Doctrine2\DBAL\Types\UuidType');
<?php
namespace BuboBox\Doctrine2\DBAL\Types;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Type that maps a PHP array to a clob SQL type.
*
* @since 2.0
*/
class UuidType extends BinaryType
{
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return sprintf('BINARY(%d) COMMENT \'(DC2Type:uuid)\'', $fieldDeclaration['length']);
}
public function getName()
{
return 'uuid';
}
public function convertToPhpValue($value, AbstractPlatform $platform)
{
if ($value !== null) {
$value= unpack('H*', $value);
$hash = array_shift($value);
$uuid = substr($hash, 0, 8) . '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
return $uuid;
}
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null) {
return pack('H*', str_replace('-', '',$value));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment