diff --git a/src/Doctrine/EntityClassGenerator.php b/src/Doctrine/EntityClassGenerator.php index a54790b77..804388bb0 100644 --- a/src/Doctrine/EntityClassGenerator.php +++ b/src/Doctrine/EntityClassGenerator.php @@ -40,7 +40,7 @@ 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(), @@ -48,7 +48,9 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $ 'Repository' ); - $tableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName()); + if (null === $tableName) { + $tableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName()); + } $useStatements = new UseStatementGenerator([ $repoClassDetails->getFullName(), diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index e0498f36b..8c92f4a17 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -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') ->setHelp($this->getHelpFileContents('MakeEntity.txt')) ; @@ -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) { diff --git a/tests/Maker/MakeEntityTest.php b/tests/Maker/MakeEntityTest.php index 54059bad3..22bc3369a 100644 --- a/tests/Maker/MakeEntityTest.php +++ b/tests/Maker/MakeEntityTest.php @@ -119,6 +119,26 @@ public function getTestDetails(): \Generator }), ]; + 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); + + $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')