Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to generate UUID V7 with the Symfony Doctrine Bridge ID generator?#53331

Unanswered
gnito-org asked this question inQ&A
Discussion options

My UUID config is as follows:

// config/packages/uid.yamlframework:uid:default_uuid_version:7time_based_uuid_version:7

./bin/console debug:config framework uid outputs:

Current configuration for "framework.uid"=========================================default_uuid_version: 7time_based_uuid_version: 7enabled: truename_based_uuid_version: 5

My entity has:

    #[ORM\Id]    #[ORM\Column(type: UuidType::NAME, unique:true)]    #[ORM\GeneratedValue(strategy:'CUSTOM')]    #[ORM\CustomIdGenerator(class:'doctrine.uuid_generator')]private ?Uuid$id;

The auto-generated entity ID UUIDs are V6, not V7.

If I inspect any of the UUID primary keys in the database with:

./bin/console uuid:inspect 1eea7e0f-d006-64a2-9024-6b5706aaf847

It outputs:

----------------------- --------------------------------------   Label                   Value                                  ----------------------- --------------------------------------   Version                 6                                       toRfc4122 (canonical)   1eea7e0f-d006-64a2-9024-6b5706aaf847    toBase58                4pRXwS8kxg34Axt2x9nG7g                  toBase32                0YX9Z0ZM06CJH9093BAW3ANY27              toHex                   0x1eea7e0fd00664a290246b5706aaf847

Question

How does one configure the Doctrine Bridge UUID generator to generate V7 UUIDs?

You must be logged in to vote

Replies: 3 comments 8 replies

Comment options

I don’t see how it could happen 🤔 could you provide a reproducer?

BTW UUID can be generated client-side, which avoids declaring your identifier nullable, which is better.

You must be logged in to vote
1 reply
@gnito-org
Comment options

It has something to do withthis UuidFactory component:

namespaceSymfony\Component\Uid\Factory;useSymfony\Component\Uid\Uuid;useSymfony\Component\Uid\UuidV1;useSymfony\Component\Uid\UuidV4;useSymfony\Component\Uid\UuidV5;useSymfony\Component\Uid\UuidV6;class UuidFactory{privatestring$defaultClass;privatestring$timeBasedClass;privatestring$nameBasedClass;privatestring$randomBasedClass;private ?Uuid$timeBasedNode;private ?Uuid$nameBasedNamespace;publicfunction__construct(string|int$defaultClass = UuidV6::class,string|int$timeBasedClass = UuidV6::class,string|int$nameBasedClass = UuidV5::class,string|int$randomBasedClass = UuidV4::class,Uuid|string$timeBasedNode =null,Uuid|string$nameBasedNamespace =null)    {if (null !==$timeBasedNode && !$timeBasedNodeinstanceof Uuid) {$timeBasedNode = Uuid::fromString($timeBasedNode);        }if (null !==$nameBasedNamespace) {$nameBasedNamespace =$this->getNamespace($nameBasedNamespace);        }$this->defaultClass =is_numeric($defaultClass) ? Uuid::class.'V'.$defaultClass :$defaultClass;$this->timeBasedClass =is_numeric($timeBasedClass) ? Uuid::class.'V'.$timeBasedClass :$timeBasedClass;$this->nameBasedClass =is_numeric($nameBasedClass) ? Uuid::class.'V'.$nameBasedClass :$nameBasedClass;$this->randomBasedClass =is_numeric($randomBasedClass) ? Uuid::class.'V'.$randomBasedClass :$randomBasedClass;$this->timeBasedNode =$timeBasedNode;$this->nameBasedNamespace =$nameBasedNamespace;    }publicfunctioncreate():Uuid    {$class =$this->defaultClass;returnnew$class();    }

TheUuidGenerator class is instantiated withnull as the$factory constructor parameter. Consequently it instantiates a vanilla instance ofUuidFactory, which then defaults to UUID::v6.

namespaceSymfony\Bridge\Doctrine\IdGenerator;useDoctrine\ORM\EntityManager;useDoctrine\ORM\EntityManagerInterface;useDoctrine\ORM\Id\AbstractIdGenerator;useSymfony\Component\Uid\Factory\NameBasedUuidFactory;useSymfony\Component\Uid\Factory\RandomBasedUuidFactory;useSymfony\Component\Uid\Factory\TimeBasedUuidFactory;useSymfony\Component\Uid\Factory\UuidFactory;useSymfony\Component\Uid\Uuid;finalclass UuidGeneratorextends AbstractIdGenerator{privatereadonlyUuidFactory$protoFactory;privateUuidFactory|NameBasedUuidFactory|RandomBasedUuidFactory|TimeBasedUuidFactory$factory;private ?string$entityGetter =null;publicfunction__construct(UuidFactory$factory =null)    {$this->protoFactory =$this->factory =$factory ??newUuidFactory();    }

When I dump the $factory parameter it is NULL:

publicfunction__construct(UuidFactory$factory =null)    {dump($factory);$this->protoFactory =$this->factory =$factory ??newUuidFactory();    }
Comment options

The work-around is to not use the auto-generation in the entity:

    #[ORM\Id]    #[ORM\Column(type: UuidType::NAME, unique:true)]private ?Uuid$id =null;publicfunction__construct()    {$this->id = Uuid::v7();    }

It works fine with no problems.

You must be logged in to vote
6 replies
@gnito-org
Comment options

OK, but that has no bearing on the issue.

@mariusfaber98
Comment options

I think this workaround is no longer needed Symfony fixed it and now generates Uuids v7

@tfink
Comment options

Nothing is fixed for me, still generating v6 by default.

@wikando-mu
Comment options

I tried to created a ownUuidV7Generator Class and used it as aCustomIdGenerator in my Doctrine Entity.

declare(strict_types=1);namespaceApp\Entity\Doctrine;useDoctrine\ORM\EntityManagerInterface;useDoctrine\ORM\Id\AbstractIdGenerator;useSymfony\Component\Uid\Uuid;useSymfony\Component\Uid\UuidV7;class UuidV7Generatorextends AbstractIdGenerator{publicfunctiongenerateId(EntityManagerInterface$em,$entity):UuidV7    {return Uuid::v7();    }}
    #[ORM\Id]    #[ORM\Column(type: UuidType::NAME, unique:true)]    #[ORM\GeneratedValue(strategy:'CUSTOM')]    #[ORM\CustomIdGenerator(class: UuidV7Generator::class)]publicreadonly Uuid$id;

With this Workaround a UuidV7 was saved in the Database.

@Tan5en5
Comment options

Hi everyone,

Same problem on our project, even with redeclaring manuallyuuid.factory anddoctrine.uuid_generator services, problem is still there.

uuid.factory:class:'Symfony\Component\Uid\Factory\UuidFactory'arguments:$defaultClass:7$timeBasedClass:7doctrine.uuid_generator:class:'Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator'arguments:$factory:'@uuid.factory'
Comment options

Seeing that this impacts many people I spent some time investigating what’s going on.

First, using the DoctrineBridgeUuidGenerator is a bad idea since you’ll get anull ID until you persist the entity. Setting it in the constructor is simpler and get rid of this issue.
Plus, quoting the docs:

Using UUIDs as primary keys is usually not recommended for performance reasons: indexes are slower and take more space


If you don’t care about those issues, you need to registerCustomIdGenerator’sclass as a service. If you want it to use the defaultuuid.factory you don’t even need to configure it.

So, for the final answer:

services:Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator:~
You must be logged in to vote
1 reply
@Tan5en5
Comment options

Many thanks for the explanations 🙂

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
6 participants
@gnito-org@MatTheCat@tfink@Tan5en5@mariusfaber98@wikando-mu

[8]ページ先頭

©2009-2025 Movatter.jp