Skip to content

Allow make:entity specify the table name #1635

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ 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, ?string $tableName = null): string
{
$repoClassDetails = $this->generator->createClassNameDetails(
$entityClassDetails->getRelativeName(),
'Repository\\',
'Repository'
);

$tableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName());
if (null === $tableName) {
Copy link

Choose a reason for hiding this comment

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

Wouldn't you consider handling the most common case of pluralizing the class name somehow? Maybe via another option or something else? You can use this Symfony inflector capability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point. But I think it's outside the scope of this PR, my purpose is to be able to explicitly choose the table name, not to change the default behavior. Both can also coexist, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I keep the idea to open a new PR in the next few days, or maybe you want to do it

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

$useStatements = new UseStatementGenerator([
$repoClassDetails->getFullName(),
Expand Down
2 changes: 2 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-name', 't', InputOption::VALUE_NONE, 'Set custom table name')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
->addOption('table-name', 't', InputOption::VALUE_NONE, 'Set custom table name')
->addOption('table-name', 't', InputOption::VALUE_REQUIRED, 'Set custom table name')

This is the reason of the test failures I think.

But i'm wondering here... should we add a test to avoid using an existing table name ?

->setHelp($this->getHelpFileContents('MakeEntity.txt'))
;

Expand Down Expand Up @@ -194,6 +195,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
apiResource: $input->getOption('api-resource'),
broadcast: $broadcast,
useUuidIdentifier: $this->getIdType(),
tableName: $input->getOption('table-name')
);

if ($broadcast) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@
}),
];

yield 'it_creates_a_new_class_with_custom_table_name' => [$this->createMakeEntityTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker([
// entity class name
'User',
// table name
'users',
// no fields
'',
]);

$this->assertFileExists($runner->getPath('src/Entity/User.php'));

$content = file_get_contents($runner->getPath('src/Entity/User.php'));
$this->assertStringContainsString('#[ORM\Table(name: users)]', $content);

Check failure on line 136 in tests/Maker/MakeEntityTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @6.4.x-dev highest deps

Failed asserting that '<?php\n

Check failure on line 136 in tests/Maker/MakeEntityTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.0.x-dev highest deps

Failed asserting that '<?php\n

Check failure on line 136 in tests/Maker/MakeEntityTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.1.x-dev highest deps

Failed asserting that '<?php\n

Check failure on line 136 in tests/Maker/MakeEntityTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* lowest deps

Failed asserting that '<?php\n

Check failure on line 136 in tests/Maker/MakeEntityTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* highest deps

Failed asserting that '<?php\n

$this->runEntityTest($runner);
}),
];

yield 'it_creates_a_new_class_and_api_resource' => [$this->createMakeEntityTest()
// @legacy - re-enable test when https://github.com/symfony/recipes/pull/1339 is merged
->skipTest('Waiting for https://github.com/symfony/recipes/pull/1339')
Expand Down
Loading