Skip to content

make:entity Add use of Doctrine types constants in attributes #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function process(ContainerBuilder $container): void
$tagAttributes = ['command' => $class::getCommandName()];

if (!method_exists($class, 'getCommandDescription')) {
// no-op
// no-op
} elseif (class_exists(LazyCommand::class)) {
$tagAttributes['description'] = $class::getCommandDescription();
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ApiPlatform\Metadata\ApiResource;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\MakerBundle\Generator;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
$useStatements = new UseStatementGenerator([
$repoClassDetails->getFullName(),
[Mapping::class => 'ORM'],
Types::class,
]);

if ($broadcast) {
Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$pantherAvailable = trait_exists(PantherTestCaseTrait::class);

$useStatements = new UseStatementGenerator([
($pantherAvailable ? PantherTestCase::class : WebTestCase::class),
$pantherAvailable ? PantherTestCase::class : WebTestCase::class,
]);

$generator->generateClass(
Expand Down
4 changes: 2 additions & 2 deletions src/Maker/MakeResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
CODE
);

$manipulator->addManyToOneRelation((new RelationManyToOne(
$manipulator->addManyToOneRelation(new RelationManyToOne(
propertyName: 'user',
targetClassName: $this->userClass,
mapInverseRelation: false,
avoidSetter: true,
isCustomReturnTypeNullable: false,
customReturnType: 'object',
isOwning: true,
)));
));

$this->fileManager->dumpFile($requestEntityPath, $manipulator->getSourceCode());

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/skeleton/doctrine/Entity.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class <?= $class_name."\n" ?>
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private int $id;

public function getId(): ?int
Expand Down
48 changes: 48 additions & 0 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public function getSourceCode(): string
public function addEntityField(string $propertyName, array $columnOptions, array $comments = []): void
{
$typeHint = $this->getEntityTypeHint($columnOptions['type']);

if (null !== $typeHint) {
$columnOptions['type'] = $this->getTypeConstant($columnOptions['type']);
}

$nullable = $columnOptions['nullable'] ?? false;
$isId = (bool) ($columnOptions['id'] ?? false);
$attributes[] = $this->buildAttributeNode(Column::class, $columnOptions, 'ORM');
Expand Down Expand Up @@ -801,6 +806,17 @@ public function buildAttributeNode(string $attributeClass, array $options, ?stri
return new Node\NullableType($option);
}

// Use the Doctrine Types constant
if ('type' === $option && str_starts_with($value, 'Types::')) {
return new Node\Arg(
new Node\Expr\ConstFetch(new Node\Name($value)),
false,
false,
[],
new Node\Identifier($option)
);
}

return new Node\Arg($context->buildNodeExprByValue($value), false, false, [], new Node\Identifier($option));
}, array_keys($options), array_values($options));

Expand Down Expand Up @@ -1040,6 +1056,38 @@ private function getEntityTypeHint(string $doctrineType): ?string
};
}

private function getTypeConstant(string $type): ?string
{
return match ($type) {
'array' => 'Types::ARRAY',
'ascii_string' => 'Types::ASCII_STRING',
'bigint' => 'Types::BIGINT',
'binary' => 'Types::BINARY',
'blob' => 'Types::BLOB',
'boolean' => 'Types::BOOLEAN',
'date' => 'Types::DATE_MUTABLE',
'date_immutable' => 'Types::DATE_IMMUTABLE',
'dateinterval' => 'Types::DATEINTERVAL',
'datetime', 'datetimetz' => 'Types::DATETIME_MUTABLE',
'datetime_immutable', 'datetimetz_immutable' => 'Types::DATETIME_IMMUTABLE',
'decimal' => 'Types::DECIMAL',
'float' => 'Types::FLOAT',
'guid' => 'Types::GUID',
'integer' => 'Types::INTEGER',
'json' => 'Types::JSON',
'object' => 'Types::OBJECT',
'simple_array' => 'Types::SIMPLE_ARRAY',
'smallint' => 'Types::SMALLINT',
'string' => 'Types::STRING',
'text' => 'Types::TEXT',
'time' => 'Types::TIME_MUTABLE',
'time_immutable' => 'Types::TIME_IMMUTABLE',
'ulid' => 'Types::ULID',
'uuid' => 'Types::UUID',
default => null,
};
}

private function isInSameNamespace(string $class): bool
{
$namespace = substr($class, 0, strrpos($class, '\\'));
Expand Down
4 changes: 2 additions & 2 deletions src/Util/YamlSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ private function changeValueInYaml($value)
// this means we need to break onto the next line

// increase(override) the indentation
$newYamlValue = "\n".$this->indentMultilineYamlArray($newYamlValue, ($this->indentationForDepths[$this->depth] + $this->getPreferredIndentationSize()));
$newYamlValue = "\n".$this->indentMultilineYamlArray($newYamlValue, $this->indentationForDepths[$this->depth] + $this->getPreferredIndentationSize());
} elseif ($this->isCurrentArrayMultiline() && $this->isCurrentArraySequence()) {
// we are a multi-line sequence, so drop to next line, indent and add "- " in front
$newYamlValue = "\n".$this->indentMultilineYamlArray('- '.$newYamlValue);
Expand Down Expand Up @@ -626,7 +626,7 @@ private function getEndOfPreviousKeyPosition($key): int
}

// find either a line break or a , that is the end of the previous key
while (\in_array(($char = substr($this->contents, $startOfKey - 1, 1)), [',', "\n"])) {
while (\in_array($char = substr($this->contents, $startOfKey - 1, 1), [',', "\n"])) {
--$startOfKey;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\MappedSuperclass]
Expand All @@ -11,16 +12,16 @@ class BaseClient

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\Column(type: 'string')]
#[ORM\Column(type: Types::STRING)]
private $name;

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

#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $magic;

public function __construct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
Expand All @@ -14,7 +15,7 @@ class Client extends BaseClient
/**
* @var string
*/
#[ORM\Column(type: 'string')]
#[ORM\Column(type: Types::STRING)]
private $apiKey;

#[ORM\ManyToMany(targetEntity: Tag::class)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Embeddable]
class Embed
{
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $val;

public function getVal(): ?int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Tag
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

public function getId(): ?int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\OneToMany(targetEntity: UserAvatar::class, mappedBy: 'user')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class UserAvatar
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'avatars', cascade: ['persist', 'remove'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class UserProfile
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\OneToOne(targetEntity: User::class, inversedBy: 'userProfile', cascade: ['persist', 'remove'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\MappedSuperclass]
Expand All @@ -11,16 +12,16 @@ class BaseClient

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\Column(type: 'string')]
#[ORM\Column(type: Types::STRING)]
private $name;

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

#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $magic;

public function __construct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
Expand All @@ -14,7 +15,7 @@ class Client extends BaseClient
/**
* @var string
*/
#[ORM\Column(type: 'string')]
#[ORM\Column(type: Types::STRING)]
private $apiKey;

#[ORM\ManyToMany(targetEntity: Tag::class)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Embeddable]
class Embed
{
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $val;

public function getVal(): ?int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Tag
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

public function getId(): ?int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\OneToMany(targetEntity: UserAvatar::class, mappedBy: 'user')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class UserAvatar
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private $id;

#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'avatars', cascade: ['persist', 'remove'])]
Expand Down
Loading