-
-
Save ricbra/eb96fb64035f5d2ffbae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\Doctrine\DBAL\Types\Type::addType('uuid', 'BuboBox\Doctrine2\DBAL\Types\UuidType'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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