Skip to content

Commit 5038082

Browse files
Deleting add and remove methods from repositories
1 parent 5c47dab commit 5038082

File tree

9 files changed

+22
-136
lines changed

9 files changed

+22
-136
lines changed

src/Resources/skeleton/crud/controller/Controller.tpl.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,18 @@ public function index(EntityManagerInterface $entityManager): Response
2929
<?php endif ?>
3030

3131
<?= $generator->generateRouteForControllerMethod('/new', sprintf('%s_new', $route_name), ['GET', 'POST']) ?>
32-
<?php if (isset($repository_full_class_name) && $generator->repositoryHasSaveAndRemoveMethods($repository_full_class_name)) { ?>
33-
public function new(Request $request, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
34-
<?php } else { ?>
3532
public function new(Request $request, EntityManagerInterface $entityManager): Response
36-
<?php } ?>
3733
{
3834
$<?= $entity_var_singular ?> = new <?= $entity_class_name ?>();
3935
$form = $this->createForm(<?= $form_class_name ?>::class, $<?= $entity_var_singular ?>);
4036
$form->handleRequest($request);
4137

42-
<?php if (isset($repository_full_class_name) && $generator->repositoryHasSaveAndRemoveMethods($repository_full_class_name)) { ?>
43-
if ($form->isSubmitted() && $form->isValid()) {
44-
$<?= $repository_var ?>->save($<?= $entity_var_singular ?>, true);
45-
46-
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
47-
}
48-
<?php } else { ?>
4938
if ($form->isSubmitted() && $form->isValid()) {
5039
$entityManager->persist($<?= $entity_var_singular ?>);
5140
$entityManager->flush();
5241

5342
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
5443
}
55-
<?php } ?>
5644

5745
<?php if ($use_render_form) { ?>
5846
return $this->renderForm('<?= $templates_path ?>/new.html.twig', [
@@ -76,28 +64,16 @@ public function show(<?= $entity_class_name ?> $<?= $entity_var_singular ?>): Re
7664
}
7765

7866
<?= $generator->generateRouteForControllerMethod(sprintf('/{%s}/edit', $entity_identifier), sprintf('%s_edit', $route_name), ['GET', 'POST']) ?>
79-
<?php if (isset($repository_full_class_name) && $generator->repositoryHasSaveAndRemoveMethods($repository_full_class_name)) { ?>
80-
public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
81-
<?php } else { ?>
8267
public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, EntityManagerInterface $entityManager): Response
83-
<?php } ?>
8468
{
8569
$form = $this->createForm(<?= $form_class_name ?>::class, $<?= $entity_var_singular ?>);
8670
$form->handleRequest($request);
8771

88-
<?php if (isset($repository_full_class_name) && $generator->repositoryHasSaveAndRemoveMethods($repository_full_class_name)) { ?>
89-
if ($form->isSubmitted() && $form->isValid()) {
90-
$<?= $repository_var ?>->save($<?= $entity_var_singular ?>, true);
91-
92-
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
93-
}
94-
<?php } else { ?>
9572
if ($form->isSubmitted() && $form->isValid()) {
9673
$entityManager->flush();
9774

9875
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
9976
}
100-
<?php } ?>
10177

10278
<?php if ($use_render_form) { ?>
10379
return $this->renderForm('<?= $templates_path ?>/edit.html.twig', [
@@ -113,22 +89,12 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va
11389
}
11490

11591
<?= $generator->generateRouteForControllerMethod(sprintf('/{%s}', $entity_identifier), sprintf('%s_delete', $route_name), ['POST']) ?>
116-
<?php if (isset($repository_full_class_name) && $generator->repositoryHasSaveAndRemoveMethods($repository_full_class_name)) { ?>
117-
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
118-
<?php } else { ?>
11992
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, EntityManagerInterface $entityManager): Response
120-
<?php } ?>
12193
{
122-
<?php if (isset($repository_full_class_name) && $generator->repositoryHasSaveAndRemoveMethods($repository_full_class_name)) { ?>
123-
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
124-
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>, true);
125-
}
126-
<?php } else { ?>
12794
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
12895
$entityManager->remove($<?= $entity_var_singular ?>);
12996
$entityManager->flush();
13097
}
131-
<?php } ?>
13298

13399
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
134100
}

src/Resources/skeleton/crud/test/Test.EntityManager.tpl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function testShow(): void
6262
$fixture->set<?= ucfirst($form_field); ?>('My Title');
6363
<?php endforeach; ?>
6464

65-
$this->repository->save($fixture, true);
65+
$this->manager->persist($fixture);
66+
$this->manager->flush();
6667

6768
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
6869

@@ -108,7 +109,7 @@ public function testRemove(): void
108109
$fixture->set<?= ucfirst($form_field); ?>('Value');
109110
<?php endforeach; ?>
110111

111-
$$this->manager->remove($fixture);
112+
$this->manager->remove($fixture);
112113
$this->manager->flush();
113114

114115
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));

src/Resources/skeleton/crud/test/Test.tpl.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ class <?= $class_name ?> extends WebTestCase<?= "\n" ?>
1010
private KernelBrowser $client;
1111
private <?= "$repository_class_name " ?>$repository;
1212
private string $path = '<?= $route_path; ?>/';
13+
private EntityManagerInterface $manager;
1314

1415
protected function setUp(): void
1516
{
1617
$this->client = static::createClient();
1718
$this->repository = static::getContainer()->get('doctrine')->getRepository(<?= $entity_class_name; ?>::class);
1819

1920
foreach ($this->repository->findAll() as $object) {
20-
$this->repository->remove($object, true);
21+
$this->manager->remove($object);
2122
}
2223
}
2324

@@ -60,7 +61,8 @@ public function testShow(): void
6061
$fixture->set<?= ucfirst($form_field); ?>('My Title');
6162
<?php endforeach; ?>
6263

63-
$this->repository->save($fixture, true);
64+
$this->manager->persist($fixture);
65+
$this->manager->flush();
6466

6567
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
6668

@@ -78,7 +80,8 @@ public function testEdit(): void
7880
$fixture->set<?= ucfirst($form_field); ?>('My Title');
7981
<?php endforeach; ?>
8082

81-
$this->repository->save($fixture, true);
83+
$this->manager->persist($fixture);
84+
$this->manager->flush();
8285

8386
$this->client->request('GET', sprintf('%s%s/edit', $this->path, $fixture->getId()));
8487

@@ -108,7 +111,8 @@ public function testRemove(): void
108111
$fixture->set<?= ucfirst($form_field); ?>('My Title');
109112
<?php endforeach; ?>
110113

111-
$this->repository->save($fixture, true);
114+
$this->manager->persist($fixture);
115+
$this->manager->flush();
112116

113117
self::assertSame($originalNumObjectsInRepository + 1, count($this->repository->findAll()));
114118

src/Resources/skeleton/doctrine/Repository.tpl.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@ public function __construct(ManagerRegistry $registry)
1818
{
1919
parent::__construct($registry, <?= $entity_class_name; ?>::class);
2020
}
21-
22-
public function save(<?= $entity_class_name ?> $entity, bool $flush = false): void
23-
{
24-
$entityManager = $this->getEntityManager();
25-
$entityManager->persist($entity);
26-
27-
if ($flush) {
28-
$entityManager->flush();
29-
}
30-
}
31-
32-
public function remove(<?= $entity_class_name ?> $entity, bool $flush = false): void
33-
{
34-
$entityManager = $this->getEntityManager();
35-
$entityManager->remove($entity);
36-
37-
if ($flush) {
38-
$entityManager->flush();
39-
}
40-
}
4121
<?php if ($include_example_comments): // When adding a new method without existing default comments, the blank line is automatically added.?>
4222

4323
<?php endif; ?>
@@ -52,8 +32,8 @@ public function upgradePassword(<?= sprintf('%s ', $password_upgrade_user_interf
5232
}
5333

5434
$user->setPassword($newHashedPassword);
55-
56-
$this->save($user, true);
35+
$this->getEntityManager()->persist($user);
36+
$this->getEntityManager()->flush();
5737
}
5838

5939
<?php endif ?>

src/Util/TemplateComponentGenerator.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,4 @@ public function getPropertyType(ClassNameDetails $classNameDetails): ?string
4343
{
4444
return sprintf('%s ', $classNameDetails->getShortName());
4545
}
46-
47-
/**
48-
* @throws \ReflectionException
49-
*/
50-
public function repositoryHasSaveAndRemoveMethods(string $repositoryFullClassName): bool
51-
{
52-
$reflectedComponents = new \ReflectionClass($repositoryFullClassName);
53-
54-
return $reflectedComponents->hasMethod('save') && $reflectedComponents->hasMethod('remove');
55-
}
5646
}

tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@ public function __construct(ManagerRegistry $registry)
2121
parent::__construct($registry, UserXml::class);
2222
}
2323

24-
public function save(UserXml $entity, bool $flush = false): void
25-
{
26-
$entityManager = $this->getEntityManager();
27-
$entityManager->persist($entity);
28-
29-
if ($flush) {
30-
$entityManager->flush();
31-
}
32-
}
33-
34-
public function remove(UserXml $entity, bool $flush = false): void
35-
{
36-
$entityManager = $this->getEntityManager();
37-
$entityManager->remove($entity);
38-
39-
if ($flush) {
40-
$entityManager->flush();
41-
}
42-
}
43-
4424
// /**
4525
// * @return UserXml[] Returns an array of UserXml objects
4626
// */

tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@ public function __construct(ManagerRegistry $registry)
2121
parent::__construct($registry, XOther::class);
2222
}
2323

24-
public function save(XOther $entity, bool $flush = false): void
25-
{
26-
$entityManager = $this->getEntityManager();
27-
$entityManager->persist($entity);
28-
29-
if ($flush) {
30-
$entityManager->flush();
31-
}
32-
}
33-
34-
public function remove(XOther $entity, bool $flush = false): void
35-
{
36-
$entityManager = $this->getEntityManager();
37-
$entityManager->remove($entity);
38-
39-
if ($flush) {
40-
$entityManager->flush();
41-
}
42-
}
43-
4424
// /**
4525
// * @return XOther[] Returns an array of XOther objects
4626
// */

tests/fixtures/make-crud/SweetFoodRepository.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,4 @@ public function __construct(ManagerRegistry $registry)
2020
{
2121
parent::__construct($registry, SweetFood::class);
2222
}
23-
24-
public function save(SweetFood $entity, bool $flush = false): void
25-
{
26-
($em = $this->getEntityManager())->persist($entity);
27-
28-
if ($flush) {
29-
$em->flush();
30-
}
31-
}
32-
33-
public function remove(SweetFood $entity, bool $flush = false): void
34-
{
35-
($em = $this->getEntityManager())->remove($entity);
36-
37-
if ($flush) {
38-
$em->flush();
39-
}
40-
}
4123
}

tests/fixtures/make-crud/expected/WithCustomRepository.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Entity\SweetFood;
66
use App\Form\SweetFoodType;
77
use App\Repository\SweetFoodRepository;
8+
use Doctrine\ORM\EntityManagerInterface;
89
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\HttpFoundation\Response;
@@ -22,14 +23,15 @@ public function index(SweetFoodRepository $sweetFoodRepository): Response
2223
}
2324

2425
#[Route('/new', name: 'app_sweet_food_new', methods: ['GET', 'POST'])]
25-
public function new(Request $request, SweetFoodRepository $sweetFoodRepository): Response
26+
public function new(Request $request, EntityManagerInterface $entityManager): Response
2627
{
2728
$sweetFood = new SweetFood();
2829
$form = $this->createForm(SweetFoodType::class, $sweetFood);
2930
$form->handleRequest($request);
3031

3132
if ($form->isSubmitted() && $form->isValid()) {
32-
$sweetFoodRepository->save($sweetFood, true);
33+
$entityManager->persist($sweetFood);
34+
$entityManager->flush();
3335

3436
return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER);
3537
}
@@ -49,13 +51,13 @@ public function show(SweetFood $sweetFood): Response
4951
}
5052

5153
#[Route('/{id}/edit', name: 'app_sweet_food_edit', methods: ['GET', 'POST'])]
52-
public function edit(Request $request, SweetFood $sweetFood, SweetFoodRepository $sweetFoodRepository): Response
54+
public function edit(Request $request, SweetFood $sweetFood, EntityManagerInterface $entityManager): Response
5355
{
5456
$form = $this->createForm(SweetFoodType::class, $sweetFood);
5557
$form->handleRequest($request);
5658

5759
if ($form->isSubmitted() && $form->isValid()) {
58-
$sweetFoodRepository->save($sweetFood, true);
60+
$entityManager->flush();
5961

6062
return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER);
6163
}
@@ -67,10 +69,11 @@ public function edit(Request $request, SweetFood $sweetFood, SweetFoodRepository
6769
}
6870

6971
#[Route('/{id}', name: 'app_sweet_food_delete', methods: ['POST'])]
70-
public function delete(Request $request, SweetFood $sweetFood, SweetFoodRepository $sweetFoodRepository): Response
72+
public function delete(Request $request, SweetFood $sweetFood, EntityManagerInterface $entityManager): Response
7173
{
7274
if ($this->isCsrfTokenValid('delete'.$sweetFood->getId(), $request->request->get('_token'))) {
73-
$sweetFoodRepository->remove($sweetFood, true);
75+
$entityManager->remove($sweetFood);
76+
$entityManager->flush();
7477
}
7578

7679
return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER);

0 commit comments

Comments
 (0)