Installation
composer require ramsey/uuid-doctrine:^2.1
Ensure ramsey/uuid (v6.x+) and doctrine/dbal (v4.x+) are installed. PHP 8.1+ is now required (breaking change).
Register the Type in Doctrine
Add the following to your config/doctrine.php (or equivalent):
'types' => [
'uuid' => Ramsey\UuidDoctrine\Types\UuidType::class,
],
First Use Case: UUID Entity Field
use Ramsey\UuidDoctrine\Types\UuidType;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'Ramsey\UuidDoctrine\UuidGenerator')]
private ?string $id = null;
}
Generate a UUID
Use the UuidGenerator in your repository or service:
$uuid = Uuid::uuid4()->toString(); // ramsey/uuid (v6.x+)
$entity->setId($uuid);
UUID as Primary Key
#[ORM\GeneratedValue(strategy: 'CUSTOM')] with Ramsey\UuidDoctrine\UuidGenerator.#[ORM\Id]
#[ORM\Column(type: 'uuid')]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'Ramsey\UuidDoctrine\UuidGenerator')]
private ?string $id;
UUID as Foreign Key
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false)]
private ?User $owner;
UUID in DTOs/Queries
UuidType in DTOs or API filters:
use Ramsey\UuidDoctrine\Types\UuidType;
class UserFilter
{
#[Assert\Type(type: UuidType::class)]
public ?string $id;
}
Bulk Inserts with UUIDs
$uuids = array_map(fn() => Uuid::uuid4()->toString(), range(1, 1000));
UuidType in form fields:
$builder->add('id', UuidType::class, ['disabled' => true]);
options: ['postgresql_uuid_binary' => true] for binary storage.UUID type; older versions require CHAR(36).CHAR(36).Database Compatibility
#[ORM\Column(type: 'uuid', options: ['postgresql_uuid_binary' => true])]
Requires PostgreSQL 12+ and doctrine/dbal v4.x.CHAR(36) instead of UUID type.CHAR(36) for UUIDs.ORM Quirks
#[ORM\GeneratedValue(strategy: 'AUTO')]—use CUSTOM with UuidGenerator.UuidGenerator.PHP Version Requirement
Serialization Issues
UuidInterface unless necessary (use Uuid::fromString()).Doctrine DBAL v4
Uuid::isValid() to validate input.Ramsey\UuidDoctrine\Types\UuidType is registered in Doctrine’s type system.UUID vs. CHAR(36)).Custom UUID Generation
Override UuidGenerator for specific formats (e.g., namespaced UUIDs):
class CustomUuidGenerator extends UuidGenerator
{
public function generate(ExecutionContext $context, ?object $entity): string
{
return Uuid::uuid5(Uuid::NAMESPACE_DNS, 'custom.prefix')->toString();
}
}
Custom Type Validation
Extend UuidType for stricter validation (e.g., reject nil UUIDs):
class StrictUuidType extends UuidType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (empty($value)) {
throw new \InvalidArgumentException('UUID cannot be empty.');
}
return parent::convertToDatabaseValue($value, $platform);
}
}
Hybrid UUID Fields
Use #[ORM\Column(type: 'string')] with UuidType for non-Doctrine contexts:
#[ORM\Column(type: 'string')]
#[Assert\Type(type: UuidType::class)]
private ?string $externalId;
PostgreSQL Binary UUIDs (v2.1.0+) Leverage PostgreSQL’s native binary UUID support:
#[ORM\Column(type: 'uuid', options: ['postgresql_uuid_binary' => true])]
private ?string $id;
Requires PostgreSQL 12+ and doctrine/dbal v4.x for optimal performance.
How can I help you explore Laravel packages today?