Skip to content
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

Allow make:entity specify the table name #1635

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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): string
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
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, EntityIdTypeEnum $useUuidIdentifier = EntityIdTypeEnum::INT, ?string $tableName): 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 @@
->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', null, InputOption::VALUE_NONE, 'Overwrite default table name')
Copy link

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', null, InputOption::VALUE_NONE, 'Overwrite default table name')
->addOption('table-name', 't', InputOption::VALUE_NONE, 'Overwrite default table name')

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

Expand Down Expand Up @@ -189,11 +190,12 @@
$classExists = class_exists($entityClassDetails->getFullName());
if (!$classExists) {
$broadcast = $input->getOption('broadcast');
$entityPath = $this->entityClassGenerator->generateEntityClass(

Check failure on line 193 in src/Maker/MakeEntity.php

View workflow job for this annotation

GitHub Actions / PHPStan

Missing parameter $generateRepositoryClass (bool) in call to method Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator::generateEntityClass().

Check failure on line 193 in src/Maker/MakeEntity.php

View workflow job for this annotation

GitHub Actions / PHPStan

Missing parameter $withPasswordUpgrade (bool) in call to method Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator::generateEntityClass().
entityClassDetails: $entityClassDetails,
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 @@ 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')
Expand Down
Loading