Skip to content

Commit b23178c

Browse files
authored
[make:entity] Improve uid support (#1139)
1 parent 5d517da commit b23178c

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

src/Maker/MakeEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
374374
} elseif (str_starts_with($snakeCasedField, 'has_')) {
375375
$defaultType = 'boolean';
376376
} elseif ('uuid' === $snakeCasedField) {
377-
$defaultType = 'uuid';
377+
$defaultType = Type::hasType('uuid') ? 'uuid' : 'guid';
378378
} elseif ('guid' === $snakeCasedField) {
379379
$defaultType = 'guid';
380380
}

src/Util/ClassSourceManipulator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToMany;
3636
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToOne;
3737
use Symfony\Bundle\MakerBundle\Str;
38+
use Symfony\Component\Uid\Ulid;
39+
use Symfony\Component\Uid\Uuid;
3840

3941
/**
4042
* @internal
@@ -94,6 +96,8 @@ public function addEntityField(string $propertyName, array $columnOptions, array
9496
$defaultValue = null;
9597
if ('array' === $typeHint) {
9698
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
99+
} elseif ('\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) {
100+
$typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1));
97101
}
98102

99103
$this->addProperty(
@@ -1030,6 +1034,8 @@ private function getEntityTypeHint(string $doctrineType): ?string
10301034
'datetime_immutable', 'datetimetz_immutable', 'date_immutable', 'time_immutable' => '\\'.\DateTimeImmutable::class,
10311035
'dateinterval' => '\\'.\DateInterval::class,
10321036
'object' => 'object',
1037+
'uuid' => '\\'.Uuid::class,
1038+
'ulid' => '\\'.Ulid::class,
10331039
default => null,
10341040
};
10351041
}

tests/Util/ClassSourceManipulatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,24 @@ public function getAddEntityFieldTests(): \Generator
279279
],
280280
'User_simple_object.php',
281281
];
282+
283+
yield 'entity_add_uuid' => [
284+
'User_simple.php',
285+
'uuid',
286+
[
287+
'type' => 'uuid',
288+
],
289+
'User_simple_uuid.php',
290+
];
291+
292+
yield 'entity_add_ulid' => [
293+
'User_simple.php',
294+
'ulid',
295+
[
296+
'type' => 'ulid',
297+
],
298+
'User_simple_ulid.php',
299+
];
282300
}
283301

284302
/**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Symfony\Component\Uid\Ulid;
7+
8+
#[ORM\Entity]
9+
class User
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column(type: 'integer')]
14+
private $id;
15+
16+
#[ORM\Column(type: 'ulid')]
17+
private $ulid;
18+
19+
public function getId(): ?int
20+
{
21+
return $this->id;
22+
}
23+
24+
public function getUlid(): ?Ulid
25+
{
26+
return $this->ulid;
27+
}
28+
29+
public function setUlid(Ulid $ulid): self
30+
{
31+
$this->ulid = $ulid;
32+
33+
return $this;
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Symfony\Component\Uid\Uuid;
7+
8+
#[ORM\Entity]
9+
class User
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column(type: 'integer')]
14+
private $id;
15+
16+
#[ORM\Column(type: 'uuid')]
17+
private $uuid;
18+
19+
public function getId(): ?int
20+
{
21+
return $this->id;
22+
}
23+
24+
public function getUuid(): ?Uuid
25+
{
26+
return $this->uuid;
27+
}
28+
29+
public function setUuid(Uuid $uuid): self
30+
{
31+
$this->uuid = $uuid;
32+
33+
return $this;
34+
}
35+
}

0 commit comments

Comments
 (0)