Skip to content

Commit 0406cc4

Browse files
committed
[Uid] Mention the Doctrine type hinting of UUID/ULID values
1 parent 3fac19f commit 0406cc4

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

components/uid.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,35 @@ entity primary keys::
168168

169169
The UUID types and generators were introduced in Symfony 5.2.
170170

171+
When using built-in Doctrine repository methods (e.g. ``findOneBy()``), Doctrine
172+
knows how to convert these UUID types to build the SQL query
173+
(e.g. ``->findOneBy(['user' => $user->getUuid()])``). However, when using DQL
174+
queries or building the query yourself, you'll need to set ``uuid`` as the type
175+
of the UUID parameters::
176+
177+
// src/Repository/ProductRepository.php
178+
179+
// ...
180+
class ProductRepository extends ServiceEntityRepository
181+
{
182+
// ...
183+
184+
public function findUserProducts(User $user): array
185+
{
186+
$qb = $this->createQueryBuilder('p')
187+
// ...
188+
// add 'uuid' as the third argument to tell Doctrine that this is an UUID
189+
->setParameter('user', $user->getUuid(), 'uuid')
190+
191+
// alternatively, you can convert it to a value compatible with
192+
// the type inferred by Doctrine
193+
->setParameter('user', $user->getUuid()->toBinary())
194+
;
195+
196+
// ...
197+
}
198+
}
199+
171200
ULIDs
172201
-----
173202

@@ -283,6 +312,35 @@ entity primary keys::
283312

284313
The ULID types and generator were introduced in Symfony 5.2.
285314

315+
When using built-in Doctrine repository methods (e.g. ``findOneBy()``), Doctrine
316+
knows how to convert these ULID types to build the SQL query
317+
(e.g. ``->findOneBy(['user' => $user->getUlid()])``). However, when using DQL
318+
queries or building the query yourself, you'll need to set ``ulid`` as the type
319+
of the ULID parameters::
320+
321+
// src/Repository/ProductRepository.php
322+
323+
// ...
324+
class ProductRepository extends ServiceEntityRepository
325+
{
326+
// ...
327+
328+
public function findUserProducts(User $user): array
329+
{
330+
$qb = $this->createQueryBuilder('p')
331+
// ...
332+
// add 'ulid' as the third argument to tell Doctrine that this is an ULID
333+
->setParameter('user', $user->getUlid(), 'ulid')
334+
335+
// alternatively, you can convert it to a value compatible with
336+
// the type inferred by Doctrine
337+
->setParameter('user', $user->getUlid()->toBinary())
338+
;
339+
340+
// ...
341+
}
342+
}
343+
286344
.. _`unique identifiers`: https://en.wikipedia.org/wiki/UID
287345
.. _`UUIDs`: https://en.wikipedia.org/wiki/Universally_unique_identifier
288346
.. _`ULIDs`: https://github.com/ulid/spec

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy