Skip to content
Open
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
9 changes: 5 additions & 4 deletions src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function __construct(
) {
}

public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, EntityIdTypeEnum $useUuidIdentifier = EntityIdTypeEnum::INT): string
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, EntityIdTypeEnum $useUuidIdentifier = EntityIdTypeEnum::INT, array $params = []): string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, EntityIdTypeEnum $useUuidIdentifier = EntityIdTypeEnum::INT, array $params = []): string
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, EntityIdTypeEnum $useUuidIdentifier = EntityIdTypeEnum::INT, string $tableName = ''): string

{
$repoClassDetails = $this->generator->createClassNameDetails(
$entityClassDetails->getRelativeName(),
'Repository\\',
'Repository'
);

$tableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName());
$potentialTableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName());

$useStatements = new UseStatementGenerator([
$repoClassDetails->getFullName(),
Expand Down Expand Up @@ -85,8 +85,9 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
'repository_class_name' => $repoClassDetails->getShortName(),
'api_resource' => $apiResource,
'broadcast' => $broadcast,
'should_escape_table_name' => $this->doctrineHelper->isKeyword($tableName),
'table_name' => $tableName,
'is_keyword' => $this->doctrineHelper->isKeyword($params['tableName']),
'table_name' => $params['tableName'],
'should_render_table_annotation' => $params['tableName'] != $potentialTableName,
'id_type' => $useUuidIdentifier,
]
);
Expand Down
16 changes: 16 additions & 0 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
->addOption('broadcast', 'b', InputOption::VALUE_NONE, 'Add the ability to broadcast entity updates using Symfony UX Turbo?')
->addOption('regenerate', null, InputOption::VALUE_NONE, 'Instead of adding new fields, simply generate the methods (e.g. getter/setter) for existing fields')
->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite any existing getter/setter methods')
->addOption('table', null, InputOption::VALUE_OPTIONAL, 'Allow specify the table name')
->setHelp($this->getHelpFileContents('MakeEntity.txt'))
;

Expand Down Expand Up @@ -186,14 +187,29 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'Entity\\'
);

if (!$input->getOption('table')) {
$potentialTableName = $this->doctrineHelper->getPotentialTableName($input->getArgument('name'));

$tableName = $io->ask(
\sprintf('Enter the database table name (e.g. `%s`)', $potentialTableName),
$potentialTableName
);
$input->setOption('table', $tableName);
}

$classExists = class_exists($entityClassDetails->getFullName());
if (!$classExists) {
$tableName = $input->getOption('table');

$broadcast = $input->getOption('broadcast');
$entityPath = $this->entityClassGenerator->generateEntityClass(
entityClassDetails: $entityClassDetails,
apiResource: $input->getOption('api-resource'),
broadcast: $broadcast,
useUuidIdentifier: $this->getIdType(),
params: [
'tableName' => $tableName,
]
Comment on lines +210 to +212
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the intention, which is to stop adding parameters to the function and use an array instead. But in that case, we'd have to revise the entire method signature; this is just a stopgap solution.

Since the class is internal, I'm convinced that the best thing to do is to add a new parameter to the method. That way, it's clearly documented. Otherwise, we'd have to use an array-shape in the phpdoc, which is less strict.

Suggested change
params: [
'tableName' => $tableName,
]
$tableName,

);

if ($broadcast) {
Expand Down
5 changes: 3 additions & 2 deletions templates/doctrine/Entity.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<?= $use_statements; ?>

#[ORM\Entity(repositoryClass: <?= $repository_class_name ?>::class)]
<?php if ($should_escape_table_name): ?>#[ORM\Table(name: '`<?= $table_name ?>`')]
<?php endif ?>
<?php if ($should_render_table_annotation): ?>
#[ORM\Table(name: '<?= $is_keyword ? '`'.$table_name.'`' : $table_name ?>')]
<?php endif; ?>
<?php if ($api_resource): ?>
#[ApiResource]
<?php endif ?>
Expand Down
Loading