Contributed by
gennadigennadigennadi
in #37678.
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
, uuid_binary
, ulid
andulid_binary
:
// src/Entity/Product.phpnamespaceApp\Entity;useDoctrine\ORM\MappingasORM;/** * @ORM\Entity(repositoryClass="App\Repository\ProductRepository") */classProduct{/** * @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:
// there are generators for UUID V1 and V6 toouseSymfony\Bridge\Doctrine\Types\UuidV4Generator;/** * @ORM\Entity(repositoryClass="App\Repository\ProductRepository") */classProduct{/** * @ORM\Id * @ORM\Column(type="uuid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UuidV4Generator::class) */private$id;// ...}useSymfony\Bridge\Doctrine\Types\UlidGenerator;/** * @ORM\Entity(repositoryClass="App\Repository\ProductRepository") */classProduct{/** * @ORM\Id * @ORM\Column(type="uuid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UlidGenerator::class) */private$id;// ...}
Sponsor the Symfony project.