@@ -166,29 +166,26 @@ type, which converts to/from UUID objects automatically::
166
166
167
167
The UUID type was introduced in Symfony 5.2.
168
168
169
- There is no generator to assign UUIDs automatically as the value of your entity
170
- primary keys, but you can use the following ::
169
+ There's also a Doctrine generator to help auto-generate UUID values for the
170
+ entity primary keys::
171
171
172
172
namespace App\Entity;
173
173
174
174
use Doctrine\ORM\Mapping as ORM;
175
+ use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
175
176
use Symfony\Component\Uid\Uuid;
176
- // ...
177
177
178
178
class User implements UserInterface
179
179
{
180
180
/**
181
181
* @ORM\Id
182
182
* @ORM\Column(type="uuid", unique=true)
183
+ * @ORM\GeneratedValue(strategy="CUSTOM")
184
+ * @ORM\CustomIdGenerator(class=UuidGenerator::class)
183
185
*/
184
186
private $id;
185
187
186
- public function __construct()
187
- {
188
- $this->id = Uuid::v4();
189
- }
190
-
191
- public function getId(): Uuid
188
+ public function getId(): ?Uuid
192
189
{
193
190
return $this->id;
194
191
}
@@ -345,12 +342,12 @@ type, which converts to/from ULID objects automatically::
345
342
There's also a Doctrine generator to help auto-generate ULID values for the
346
343
entity primary keys::
347
344
345
+ namespace App\Entity;
346
+
347
+ use Doctrine\ORM\Mapping as ORM;
348
348
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
349
349
use Symfony\Component\Uid\Ulid;
350
350
351
- /**
352
- * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
353
- */
354
351
class Product
355
352
{
356
353
/**
@@ -361,8 +358,6 @@ entity primary keys::
361
358
*/
362
359
private $id;
363
360
364
- // ...
365
-
366
361
public function getId(): ?Ulid
367
362
{
368
363
return $this->id;
0 commit comments