Skip to content

[Uid] Mention the Doctrine type hinting of UUID/ULID values #14693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions components/uid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,35 @@ entity primary keys::

The UUID types and generators were introduced in Symfony 5.2.

When using built-in Doctrine repository methods (e.g. ``findOneBy()``), Doctrine
knows how to convert these UUID types to build the SQL query
(e.g. ``->findOneBy(['user' => $user->getUuid()])``). However, when using DQL
queries or building the query yourself, you'll need to set ``uuid`` as the type
of the UUID parameters::

// src/Repository/ProductRepository.php

// ...
class ProductRepository extends ServiceEntityRepository
{
// ...

public function findUserProducts(User $user): array
{
$qb = $this->createQueryBuilder('p')
// ...
// add 'uuid' as the third argument to tell Doctrine that this is an UUID
->setParameter('user', $user->getUuid(), 'uuid')

// alternatively, you can convert it to a value compatible with
// the type inferred by Doctrine
->setParameter('user', $user->getUuid()->toBinary())
;

// ...
}
}

ULIDs
-----

Expand Down Expand Up @@ -283,6 +312,35 @@ entity primary keys::

The ULID types and generator were introduced in Symfony 5.2.

When using built-in Doctrine repository methods (e.g. ``findOneBy()``), Doctrine
knows how to convert these ULID types to build the SQL query
(e.g. ``->findOneBy(['user' => $user->getUlid()])``). However, when using DQL
queries or building the query yourself, you'll need to set ``ulid`` as the type
of the ULID parameters::

// src/Repository/ProductRepository.php

// ...
class ProductRepository extends ServiceEntityRepository
{
// ...

public function findUserProducts(User $user): array
{
$qb = $this->createQueryBuilder('p')
// ...
// add 'ulid' as the third argument to tell Doctrine that this is an ULID
->setParameter('user', $user->getUlid(), 'ulid')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doctrine recommends to use constants in the documentation, is there an equivalent for ULID?

<?php

use Doctrine\DBAL\Types\Types;

// prevents attempt to load metadata for date time class, improving performance
$qb->setParameter('date', new \DateTimeImmutable(), Types::DATE_IMMUTABLE)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see the type in DBAL. There's guid but not uuid or ulid. See https://github.com/doctrine/dbal/blob/2.12.x/lib/Doctrine/DBAL/Types/Types.php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does ulid require another dependency that Doctrine ?

It looks like GUID and UUID could be used because they are synonyms:

/**
 * Represents a GUID/UUID datatype (both are actually synonyms) in the database.
 */
class GuidType extends StringType
{

Source: https://github.com/doctrine/dbal/blob/9e37d1bdc1ee589e9baab2bb16aebf6e16a18a92/lib/Doctrine/DBAL/Types/GuidType.php#L8

But I dont know for ULID and I don't want to block the merging of this PR, feel free to keep the current code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's sad that we don't have an ULID type ... so let's use strings for now. Thanks!


// alternatively, you can convert it to a value compatible with
// the type inferred by Doctrine
->setParameter('user', $user->getUlid()->toBinary())
;

// ...
}
}

.. _`unique identifiers`: https://en.wikipedia.org/wiki/UID
.. _`UUIDs`: https://en.wikipedia.org/wiki/Universally_unique_identifier
.. _`ULIDs`: https://github.com/ulid/spec