how to use UUID with binary in query builder code example

Example: 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);
    }
}

Tags:

Misc Example