Answers for "how to use UUID with binary in query builder"

0

how to use UUID with binary in query builder

<?php

declare(strict_types=1);

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Ramsey\Uuid\Doctrine\UuidBinaryType as BaseType;
use Ramsey\Uuid\Uuid;

/**
 * This extends the UuidBinaryType from ramsey/uuid-doctrine to work around a
 * Doctrine ORM bug introduced in v2.6.4 by overloading convertToDatabaseValue()
 * to handle values that are already binary strings.
 *
 * @see https://github.com/doctrine/orm/issues/7830
 */
final class UuidBinaryType extends BaseType
{
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        // If $value is a string but isn't a valid UUID pattern, assume it's already a binary string
        if (\is_string($value) && !\preg_match('/'.Uuid::VALID_PATTERN.'/', $value)) {
            return $value;
        }

        return parent::convertToDatabaseValue($value, $platform);
    }
}
Posted by: Guest on September-23-2020

Code answers related to "how to use UUID with binary in query builder"

Browse Popular Code Answers by Language