New in Symfony 5.2: Doctrine types for UUID and ULID
September 24, 2020 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
In Symfony 5.1 we introduced a new Uid component to help you generate and work with different UID values, such as UUIDs and ULIDs. The next step is to improve its integration with other Symfony components.
That's why in Symfony 5.2 we've added Doctrine types and generators for UUIDs
and ULIDs. You only have to install Doctrine in your Symfony application
and the new types will be available as uuid
and ulid
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// src/Entity/Product.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
/**
* @ORM\Column(type="uuid")
*/
private $someProperty;
/**
* @ORM\Column(type="ulid")
*/
private $anotherProperty;
// ...
}
When using those types, the value of the properties will be transformed from/to UUID/ULID objects automatically for you. You can also generate UUID/ULID values for your primary keys using the new generators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// there are generators for UUID V1 and V6 too
use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidV4Generator::class)
*/
private $id;
// ...
}
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="ulid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UlidGenerator::class)
*/
private $id;
// ...
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Hi! shouldn't the last example have
type="ulid"
?Thanks for your post @gennadigennadigennadi :)
I agree with Nicolas. Also the use statement and customIdGenerator show`s it should be ulid.
I've just fixed the issue in the last code example. Thanks!
What will the namespace be with v5? - and is it configurable pr entity - pr project etc?
Nice feature thanks. Although the binary versions don't currently work. Here is PR: https://github.com/symfony/symfony/pull/38292
@Martin Aarhof V3/v5 are not supported here. Would you mind sharing your ideas on an issue about how they could be used in this case? Maybe you'd be up to give it a try?