Skip to content

Commit 90ab4b5

Browse files
committed
only handle attribute nodes
1 parent fbcfc13 commit 90ab4b5

File tree

17 files changed

+61
-29
lines changed

17 files changed

+61
-29
lines changed

src/Doctrine/EntityClassGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use ApiPlatform\Metadata\ApiResource;
1515
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
16+
use Doctrine\DBAL\Types\Types;
1617
use Doctrine\ORM\Mapping;
1718
use Doctrine\Persistence\ManagerRegistry;
1819
use Symfony\Bundle\MakerBundle\Generator;
@@ -49,6 +50,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
4950
$useStatements = new UseStatementGenerator([
5051
$repoClassDetails->getFullName(),
5152
[Mapping::class => 'ORM'],
53+
Types::class,
5254
]);
5355

5456
if ($broadcast) {

src/Util/ClassSourceManipulator.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public function getSourceCode(): string
8989
public function addEntityField(string $propertyName, array $columnOptions, array $comments = []): void
9090
{
9191
$typeHint = $this->getEntityTypeHint($columnOptions['type']);
92+
93+
if (null !== $typeHint) {
94+
$columnOptions['type'] = $this->getTypeConstant($columnOptions['type']);
95+
}
96+
9297
$nullable = $columnOptions['nullable'] ?? false;
9398
$isId = (bool) ($columnOptions['id'] ?? false);
9499
$attributes[] = $this->buildAttributeNode(Column::class, $columnOptions, 'ORM');
@@ -801,6 +806,17 @@ public function buildAttributeNode(string $attributeClass, array $options, ?stri
801806
return new Node\NullableType($option);
802807
}
803808

809+
// Use the Doctrine Types constant
810+
if ('type' === $option && str_starts_with($value, 'Types::')) {
811+
return new Node\Arg(
812+
new Node\Expr\ConstFetch(new Node\Name($value)),
813+
false,
814+
false,
815+
[],
816+
new Node\Identifier($option)
817+
);
818+
}
819+
804820
return new Node\Arg($context->buildNodeExprByValue($value), false, false, [], new Node\Identifier($option));
805821
}, array_keys($options), array_values($options));
806822

@@ -1040,9 +1056,9 @@ private function getEntityTypeHint(string $doctrineType): ?string
10401056
};
10411057
}
10421058

1043-
private function getTypeConstant(string $type): string
1059+
private function getTypeConstant(string $type): ?string
10441060
{
1045-
$typesMapping = [
1061+
return match ($type) {
10461062
'array' => 'Types::ARRAY',
10471063
'ascii_string' => 'Types::ASCII_STRING',
10481064
'bigint' => 'Types::BIGINT',
@@ -1052,10 +1068,8 @@ private function getTypeConstant(string $type): string
10521068
'date' => 'Types::DATE_MUTABLE',
10531069
'date_immutable' => 'Types::DATE_IMMUTABLE',
10541070
'dateinterval' => 'Types::DATEINTERVAL',
1055-
'datetime' => 'Types::DATETIME_MUTABLE',
1056-
'datetime_immutable' => 'Types::DATETIME_IMMUTABLE',
1057-
'datetimetz' => 'Types::DATETIMETZ_MUTABLE',
1058-
'datetimetz_immutable' => 'Types::DATETIMETZ_IMMUTABLE',
1071+
'datetime', 'datetimetz' => 'Types::DATETIME_MUTABLE',
1072+
'datetime_immutable', 'datetimetz_immutable' => 'Types::DATETIME_IMMUTABLE',
10591073
'decimal' => 'Types::DECIMAL',
10601074
'float' => 'Types::FLOAT',
10611075
'guid' => 'Types::GUID',
@@ -1068,9 +1082,10 @@ private function getTypeConstant(string $type): string
10681082
'text' => 'Types::TEXT',
10691083
'time' => 'Types::TIME_MUTABLE',
10701084
'time_immutable' => 'Types::TIME_IMMUTABLE',
1071-
];
1072-
1073-
return $typesMapping[$type] ?? $type;
1085+
'ulid' => 'Types::ULID',
1086+
'uuid' => 'Types::UUID',
1087+
default => null,
1088+
};
10741089
}
10751090

10761091
private function isInSameNamespace(string $class): bool

tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/BaseClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Bundle\MakerBundle\Tests\tmp\current_project\src\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\MappedSuperclass]
@@ -11,16 +12,16 @@ class BaseClient
1112

1213
#[ORM\Id]
1314
#[ORM\GeneratedValue]
14-
#[ORM\Column(type: 'integer')]
15+
#[ORM\Column(type: Types::INTEGER)]
1516
private $id;
1617

17-
#[ORM\Column(type: 'string')]
18+
#[ORM\Column(type: Types::STRING)]
1819
private $name;
1920

2021
#[ORM\ManyToOne(targetEntity: User::class)]
2122
private $creator;
2223

23-
#[ORM\Column(type: 'integer')]
24+
#[ORM\Column(type: Types::INTEGER)]
2425
private $magic;
2526

2627
public function __construct()

tests/Util/fixtures/add_class_attribute/User_simple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67
use Doctrine\ORM\Mapping\Column;
78

@@ -11,7 +12,7 @@ class User
1112
{
1213
#[ORM\Id]
1314
#[ORM\GeneratedValue]
14-
#[ORM\Column(type: 'integer')]
15+
#[ORM\Column(type: Types::INTEGER)]
1516
private $id;
1617

1718
public function getId(): ?int

tests/Util/fixtures/add_constructor/UserSimple_with_constructor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
public function __construct(object $someObjectParam, string $someStringParam)

tests/Util/fixtures/add_entity_field/User_simple_object.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

15-
#[ORM\Column(type: 'object')]
16+
#[ORM\Column(type: Types::OBJECT)]
1617
private $someObject;
1718

1819
public function getId(): ?int

tests/Util/fixtures/add_entity_field/User_simple_ulid.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67
use Symfony\Component\Uid\Ulid;
78

@@ -10,10 +11,10 @@ class User
1011
{
1112
#[ORM\Id]
1213
#[ORM\GeneratedValue]
13-
#[ORM\Column(type: 'integer')]
14+
#[ORM\Column(type: Types::INTEGER)]
1415
private $id;
1516

16-
#[ORM\Column(type: 'ulid')]
17+
#[ORM\Column(type: Types::ULID)]
1718
private $ulid;
1819

1920
public function getId(): ?int

tests/Util/fixtures/add_entity_field/User_simple_uuid.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67
use Symfony\Component\Uid\Uuid;
78

@@ -10,10 +11,10 @@ class User
1011
{
1112
#[ORM\Id]
1213
#[ORM\GeneratedValue]
13-
#[ORM\Column(type: 'integer')]
14+
#[ORM\Column(type: Types::INTEGER)]
1415
private $id;
1516

16-
#[ORM\Column(type: 'uuid')]
17+
#[ORM\Column(type: Types::UUID)]
1718
private $uuid;
1819

1920
public function getId(): ?int

tests/Util/fixtures/add_getter/User_simple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
public function getId(): ?int

tests/Util/fixtures/add_getter/User_simple_bool.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
public function getId(): ?int

tests/Util/fixtures/add_property/User_simple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
private $fooProp;

tests/Util/fixtures/add_setter/User_simple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
public function getId(): ?int

tests/Util/fixtures/add_trait/User_with_prop_trait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Entity;
44

55
use App\TestTrait;
6+
use Doctrine\DBAL\Types\Types;
67
use Doctrine\ORM\Mapping as ORM;
78

89
#[ORM\Entity]
@@ -12,7 +13,7 @@ class User
1213

1314
#[ORM\Id]
1415
#[ORM\GeneratedValue]
15-
#[ORM\Column(type: 'integer')]
16+
#[ORM\Column(type: Types::INTEGER)]
1617
private $id;
1718

1819
public function getId(): ?int

tests/Util/fixtures/implements_interface/User_simple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67
use Symfony\Component\Security\Core\User\UserInterface;
78

@@ -10,7 +11,7 @@ class User implements UserInterface
1011
{
1112
#[ORM\Id]
1213
#[ORM\GeneratedValue]
13-
#[ORM\Column(type: 'integer')]
14+
#[ORM\Column(type: Types::INTEGER)]
1415
private $id;
1516

1617
public function getId(): ?int

tests/Util/fixtures/implements_interface/User_simple_with_interface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67
use Symfony\Component\Security\Core\User\UserInterface;
78

@@ -10,7 +11,7 @@ class User implements DummyInterface, UserInterface
1011
{
1112
#[ORM\Id]
1213
#[ORM\GeneratedValue]
13-
#[ORM\Column(type: 'integer')]
14+
#[ORM\Column(type: Types::INTEGER)]
1415
private $id;
1516

1617
public function getId(): ?int

tests/Util/fixtures/source/User_only_props.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
/**

tests/Util/fixtures/source/User_simple_with_interface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Entity;
44

5+
use Doctrine\DBAL\Types\Types;
56
use Doctrine\ORM\Mapping as ORM;
67

78
#[ORM\Entity]
89
class User implements DummyInterface
910
{
1011
#[ORM\Id]
1112
#[ORM\GeneratedValue]
12-
#[ORM\Column(type: 'integer')]
13+
#[ORM\Column(type: Types::INTEGER)]
1314
private $id;
1415

1516
public function getId(): ?int

0 commit comments

Comments
 (0)