Skip to content

[make:entity] better repo methods #1312

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
wants to merge 1 commit into from
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
8 changes: 5 additions & 3 deletions src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
) {
}

public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false): string
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, bool $useLegacyRepoMethods = false): string
{
$repoClassDetails = $this->generator->createClassNameDetails(
$entityClassDetails->getRelativeName(),
Expand Down Expand Up @@ -77,14 +77,15 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
$repoClassDetails->getFullName(),
$entityClassDetails->getFullName(),
$withPasswordUpgrade,
true
true,
$useLegacyRepoMethods
);
}

return $entityPath;
}

public function generateRepositoryClass(string $repositoryClass, string $entityClass, bool $withPasswordUpgrade, bool $includeExampleComments = true): void
public function generateRepositoryClass(string $repositoryClass, string $entityClass, bool $withPasswordUpgrade, bool $includeExampleComments = true, bool $useLegacyMethods = false): void
{
$shortEntityClass = Str::getShortClassName($entityClass);
$entityAlias = strtolower($shortEntityClass[0]);
Expand Down Expand Up @@ -121,6 +122,7 @@ public function generateRepositoryClass(string $repositoryClass, string $entityC
'with_password_upgrade' => $withPasswordUpgrade,
'password_upgrade_user_interface' => $interfaceClassNameDetails,
'include_example_comments' => $includeExampleComments,
'use_legacy_methods' => $useLegacyMethods,
]
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,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('legacy-methods', null, InputOption::VALUE_NONE, 'Generate repository with legacy <fg=yellow>save()</> and <fg=yellow>remove()</> methods.')
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeEntity.txt'))
;

Expand Down
29 changes: 29 additions & 0 deletions src/Resources/skeleton/doctrine/Repository.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, <?= $entity_class_name; ?>::class);
}

<?php if ($use_legacy_methods): ?>
public function save(<?= $entity_class_name ?> $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
Expand All @@ -36,6 +37,34 @@ public function remove(<?= $entity_class_name ?> $entity, bool $flush = false):
$this->getEntityManager()->flush();
}
}
<?php else: ?>
public function persist(<?= $entity_class_name ?> $entity): void
{
$this->_em->persist($entity);
}

public function persistAndFlush(<?= $entity_class_name ?> $entity): void
{
$this->persist($entity);
$this->flush();
}

public function flush(): void
{
$this->_em->flush();
}

public function remove(<?= $entity_class_name ?> $entity): void
{
$this->_em->remove($entity);
}

public function removeAndFlush(<?= $entity_class_name ?> $entity): void
{
$this->remove($entity);
$this->flush();
}
<?php endif; ?>
<?php if ($include_example_comments): // When adding a new method without existing default comments, the blank line is automatically added.?>

<?php endif; ?>
Expand Down