Skip to content

Commit bb1566e

Browse files
committed
feat(entity): Add use of Doctrine types constants in attributes
1 parent 716eee9 commit bb1566e

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Resources/skeleton/doctrine/Entity.tpl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<?php endif ?>
88
use <?= $repository_full_class_name ?>;
99
use Doctrine\ORM\Mapping as ORM;
10+
<?php if ($doctrine_use_attributes): ?>use Doctrine\DBAL\Types\Types;
11+
<?php endif ?>
1012
<?php if ($broadcast): ?>use Symfony\UX\Turbo\Attribute\Broadcast;
1113
<?php endif ?>
1214

@@ -41,7 +43,7 @@ class <?= $class_name."\n" ?>
4143
*/
4244
<?php else: ?>#[ORM\Id]
4345
#[ORM\GeneratedValue]
44-
#[ORM\Column(type: 'integer')]
46+
#[ORM\Column(type: Types::INTEGER)]
4547
<?php endif ?>private $id;
4648

4749
public function getId(): ?int

src/Util/ClassSourceManipulator.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,39 @@ private function getEntityTypeHint($doctrineType): ?string
11621162
}
11631163
}
11641164

1165+
private function getTypeConstant(string $type)
1166+
{
1167+
$typesMapping = [
1168+
'array' => 'Types::ARRAY',
1169+
'ascii_string' => 'Types::ASCII_STRING',
1170+
'bigint' => 'Types::BIGINT',
1171+
'binary' => 'Types::BINARY',
1172+
'blob' => 'Types::BLOB',
1173+
'boolean' => 'Types::BOOLEAN',
1174+
'date' => 'Types::DATE_MUTABLE',
1175+
'date_immutable' => 'Types::DATE_IMMUTABLE',
1176+
'dateinterval' => 'Types::DATEINTERVAL',
1177+
'datetime' => 'Types::DATETIME_MUTABLE',
1178+
'datetime_immutable' => 'Types::DATETIME_IMMUTABLE',
1179+
'datetimetz' => 'Types::DATETIMETZ_MUTABLE',
1180+
'datetimetz_immutable' => 'Types::DATETIMETZ_IMMUTABLE',
1181+
'decimal' => 'Types::DECIMAL',
1182+
'float' => 'Types::FLOAT',
1183+
'guid' => 'Types::GUID',
1184+
'integer' => 'Types::INTEGER',
1185+
'json' => 'Types::JSON',
1186+
'object' => 'Types::OBJECT',
1187+
'simple_array' => 'Types::SIMPLE_ARRAY',
1188+
'smallint' => 'Types::SMALLINT',
1189+
'string' => 'Types::STRING',
1190+
'text' => 'Types::TEXT',
1191+
'time' => 'Types::TIME_MUTABLE',
1192+
'time_immutable' => 'Types::TIME_IMMUTABLE',
1193+
];
1194+
1195+
return $typesMapping[$type] ?? $type;
1196+
}
1197+
11651198
private function isInSameNamespace($class): bool
11661199
{
11671200
$namespace = substr($class, 0, strrpos($class, '\\'));
@@ -1432,7 +1465,22 @@ private function buildAttributeNode(string $attributeClass, array $options): Nod
14321465
$options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass);
14331466

14341467
$context = $this;
1435-
$nodeArguments = array_map(static function ($option, $value) use ($context) {
1468+
$nodeArguments = array_map(static function ($option, $value) use ($context, $attributeClass) {
1469+
// if we do are creating an ORM column attribute and this is the type option, we replace string with its constant
1470+
if ('type' === $option && 'ORM\\Column' === $attributeClass) {
1471+
$typeConstant = $context->getTypeConstant($value);
1472+
// if given type is handled by doctrine, we generate the attribute using the constant
1473+
if ($typeConstant !== $value) {
1474+
return new Node\Arg(
1475+
new Node\Expr\ConstFetch(new Node\Name($typeConstant)),
1476+
false,
1477+
false,
1478+
[],
1479+
new Node\Identifier($option)
1480+
);
1481+
}
1482+
}
1483+
14361484
return new Node\Arg($context->buildNodeExprByValue($value), false, false, [], new Node\Identifier($option));
14371485
}, array_keys($options), array_values($options));
14381486

0 commit comments

Comments
 (0)