@@ -112,6 +112,62 @@ UUID objects created with the ``Uuid`` class can use the following methods
112112 // * int < 0 if $uuid1 is less than $uuid4
113113 $uuid1->compare($uuid4); // e.g. int(4)
114114
115+ Storing UUIDs in Databases
116+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
117+
118+ You can store UUID values as any other regular string/binary values in the database.
119+ However, if you :doc: `use Doctrine </doctrine >`, it's more convenient to use the
120+ special Doctrine types which convert to/from UUID objects automatically::
121+
122+ // src/Entity/Product.php
123+ namespace App\Entity;
124+
125+ use Doctrine\ORM\Mapping as ORM;
126+
127+ /**
128+ * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
129+ */
130+ class Product
131+ {
132+ /**
133+ * @ORM\Column(type="uuid")
134+ */
135+ private $someProperty;
136+
137+ /**
138+ * @ORM\Column(type="uuid_binary")
139+ */
140+ private $anotherProperty;
141+
142+ // ...
143+ }
144+
145+ There's also a Doctrine generator to help autogenerate UUID values for the
146+ entity primary keys::
147+
148+ // there are generators for UUID V1 and V6 too
149+ use Symfony\Bridge\Doctrine\Types\UuidV4Generator;
150+
151+ /**
152+ * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
153+ */
154+ class Product
155+ {
156+ /**
157+ * @ORM\Id
158+ * @ORM\Column(type="uuid", unique=true)
159+ * @ORM\GeneratedValue(strategy="CUSTOM")
160+ * @ORM\CustomIdGenerator(class=UuidV4Generator::class)
161+ */
162+ private $id;
163+
164+ // ...
165+ }
166+
167+ .. versionadded :: 5.2
168+
169+ The UUID types and generators were introduced in Symfony 5.2.
170+
115171ULIDs
116172-----
117173
@@ -172,6 +228,61 @@ ULID objects created with the ``Ulid`` class can use the following methods::
172228 // this method returns $ulid1 <=> $ulid2
173229 $ulid1->compare($ulid2); // e.g. int(-1)
174230
231+ Storing ULIDs in Databases
232+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
233+
234+ You can store ULID values as any other regular string/binary values in the database.
235+ However, if you :doc: `use Doctrine </doctrine >`, it's more convenient to use the
236+ special Doctrine types which convert to/from ULID objects automatically::
237+
238+ // src/Entity/Product.php
239+ namespace App\Entity;
240+
241+ use Doctrine\ORM\Mapping as ORM;
242+
243+ /**
244+ * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
245+ */
246+ class Product
247+ {
248+ /**
249+ * @ORM\Column(type="ulid")
250+ */
251+ private $someProperty;
252+
253+ /**
254+ * @ORM\Column(type="ulid_binary")
255+ */
256+ private $anotherProperty;
257+
258+ // ...
259+ }
260+
261+ There's also a Doctrine generator to help autogenerate ULID values for the
262+ entity primary keys::
263+
264+ use Symfony\Bridge\Doctrine\Types\UlidGenerator;
265+
266+ /**
267+ * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
268+ */
269+ class Product
270+ {
271+ /**
272+ * @ORM\Id
273+ * @ORM\Column(type="uuid", unique=true)
274+ * @ORM\GeneratedValue(strategy="CUSTOM")
275+ * @ORM\CustomIdGenerator(class=UlidGenerator::class)
276+ */
277+ private $id;
278+
279+ // ...
280+ }
281+
282+ .. versionadded :: 5.2
283+
284+ The ULID types and generator were introduced in Symfony 5.2.
285+
175286.. _`unique identifiers` : https://en.wikipedia.org/wiki/UID
176287.. _`UUIDs` : https://en.wikipedia.org/wiki/Universally_unique_identifier
177288.. _`ULIDs` : https://github.com/ulid/spec
0 commit comments