Skip to content

Commit 1560455

Browse files
committed
[make:crud] fix broken controller with custom repository
1 parent 4b994df commit 1560455

15 files changed

+470
-64
lines changed

src/Maker/MakeCrud.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
119119
$repositoryClassName = $repositoryClassDetails->getFullName();
120120

121121
$repositoryVars = [
122+
'repository_full_class_name' => $repositoryClassName,
122123
'repository_class_name' => $repositoryClassDetails->getShortName(),
123124
'repository_var' => lcfirst($this->inflector->singularize($repositoryClassDetails->getShortName())),
124125
];

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
4747

4848
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
4949
if ($form->isSubmitted() && $form->isValid()) {
50-
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
50+
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>, true);
51+
5152
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
5253
}
5354
<?php } else { ?>
@@ -92,7 +93,8 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va
9293

9394
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
9495
if ($form->isSubmitted() && $form->isValid()) {
95-
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
96+
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>, true);
97+
9698
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
9799
}
98100
<?php } else { ?>
@@ -125,7 +127,7 @@ public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_
125127
{
126128
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
127129
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
128-
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>);
130+
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>, true);
129131
}
130132
<?php } else { ?>
131133
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {

tests/Maker/MakeCrudTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
class MakeCrudTest extends MakerTestCase
2020
{
21+
use TestHelpersTrait;
22+
2123
protected function getMakerClass(): string
2224
{
2325
return MakeCrud::class;
@@ -28,7 +30,7 @@ public function getTestDetails(): \Generator
2830
yield 'it_generates_basic_crud' => [$this->createMakerTest()
2931
->run(function (MakerTestRunner $runner) {
3032
$runner->copy(
31-
'make-crud/SweetFood.php',
33+
$this->getFixturePath('SweetFood.php', $runner),
3234
'src/Entity/SweetFood.php'
3335
);
3436

@@ -48,7 +50,7 @@ public function getTestDetails(): \Generator
4850
yield 'it_generates_crud_with_custom_controller' => [$this->createMakerTest()
4951
->run(function (MakerTestRunner $runner) {
5052
$runner->copy(
51-
'make-crud/SweetFood.php',
53+
$this->getFixturePath('SweetFood.php', $runner),
5254
'src/Entity/SweetFood.php'
5355
);
5456

@@ -74,7 +76,7 @@ public function getTestDetails(): \Generator
7476
);
7577

7678
$runner->copy(
77-
'make-crud/SweetFood-custom-namespace.php',
79+
$this->getFixturePath('SweetFood-custom-namespace.php', $runner),
7880
'src/Entity/SweetFood.php'
7981
);
8082

@@ -94,7 +96,7 @@ public function getTestDetails(): \Generator
9496
yield 'it_generates_crud_using_custom_repository' => [$this->createMakerTest()
9597
->run(function (MakerTestRunner $runner) {
9698
$runner->copy(
97-
'make-crud/SweetFood.php',
99+
$this->getFixturePath('SweetFoodCustomRepository.php', $runner),
98100
'src/Entity/SweetFood.php'
99101
);
100102
$runner->copy(
@@ -112,13 +114,18 @@ public function getTestDetails(): \Generator
112114
$this->assertStringContainsString('created: src/Form/SweetFoodType.php', $output);
113115

114116
$this->runCrudTest($runner, 'it_generates_basic_crud.php');
117+
118+
self::assertFileEquals(
119+
sprintf('%s/fixtures/%s', \dirname(__DIR__), $this->getFixturePath('expected/WithCustomRepository.php', $runner)),
120+
$runner->getPath('src/Controller/SweetFoodController.php')
121+
);
115122
}),
116123
];
117124

118125
yield 'it_generates_crud_with_no_base_template' => [$this->createMakerTest()
119126
->run(function (MakerTestRunner $runner) {
120127
$runner->copy(
121-
'make-crud/SweetFood.php',
128+
$this->getFixturePath('SweetFood.php', $runner),
122129
'src/Entity/SweetFood.php'
123130
);
124131

@@ -138,6 +145,14 @@ public function getTestDetails(): \Generator
138145
];
139146
}
140147

148+
// @legacy - remove when annotations are no longer supported
149+
private function getFixturePath(string $sourceName, MakerTestRunner $runner): string
150+
{
151+
$path = $this->useAttributes($runner) ? 'make-crud' : 'make-crud/legacy';
152+
153+
return sprintf('%s/%s', $path, $sourceName);
154+
}
155+
141156
private function runCrudTest(MakerTestRunner $runner, string $filename): void
142157
{
143158
$runner->copy(

tests/Maker/MakeEntityTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Tests\Maker;
1313

14-
use Doctrine\ORM\Mapping\Driver\AttributeReader;
1514
use Symfony\Bundle\MakerBundle\Maker\MakeEntity;
1615
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
1716
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails;
@@ -21,6 +20,8 @@
2120

2221
class MakeEntityTest extends MakerTestCase
2322
{
23+
use TestHelpersTrait;
24+
2425
protected function getMakerClass(): string
2526
{
2627
return MakeEntity::class;
@@ -671,13 +672,6 @@ private function changeToXmlMapping(MakerTestRunner $runner): void
671672
);
672673
}
673674

674-
private function useAttributes(MakerTestRunner $runner): bool
675-
{
676-
return \PHP_VERSION_ID >= 80000
677-
&& $runner->doesClassExist(AttributeReader::class)
678-
&& $runner->getSymfonyVersion() >= 50200;
679-
}
680-
681675
private function copyEntity(MakerTestRunner $runner, string $filename): void
682676
{
683677
$entityClassName = substr(

tests/Maker/TestHelpersTrait.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Tests\Maker;
13+
14+
use Doctrine\ORM\Mapping\Driver\AttributeReader;
15+
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;
16+
17+
/**
18+
* @author Jesse Rushlow <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
trait TestHelpersTrait
23+
{
24+
// @legacy - remove when annotations are no longer supported
25+
protected function useAttributes(MakerTestRunner $runner): bool
26+
{
27+
return \PHP_VERSION_ID >= 80000
28+
&& $runner->doesClassExist(AttributeReader::class);
29+
}
30+
}

tests/fixtures/make-crud/SweetFood-custom-namespace.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,31 @@
44

55
use Doctrine\ORM\Mapping as ORM;
66

7-
/**
8-
* @ORM\Entity()
9-
*/
7+
#[ORM\Entity()]
108
class SweetFood
119
{
12-
/**
13-
* @ORM\Column(name="id", type="integer")
14-
* @ORM\Id
15-
* @ORM\GeneratedValue(strategy="AUTO")
16-
*/
10+
#[ORM\Id]
11+
#[ORM\GeneratedValue]
12+
#[ORM\Column(type: 'integer')]
1713
private $id;
1814

19-
/**
20-
* @ORM\Column(name="title", type="string", length=255)
21-
*/
15+
#[ORM\Column(type: 'string', length: 255)]
2216
private $title;
2317

2418
public function getId()
2519
{
2620
return $this->id;
2721
}
2822

29-
/**
30-
* @return mixed
31-
*/
32-
public function getTitle()
23+
public function getTitle(): string
3324
{
3425
return $this->title;
3526
}
3627

37-
/**
38-
* @param mixed $title
39-
*/
40-
public function setTitle($title)
28+
public function setTitle(string $title): self
4129
{
4230
$this->title = $title;
31+
32+
return $this;
4333
}
4434
}

tests/fixtures/make-crud/SweetFood.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,31 @@
44

55
use Doctrine\ORM\Mapping as ORM;
66

7-
/**
8-
* @ORM\Entity()
9-
*/
7+
#[ORM\Entity()]
108
class SweetFood
119
{
12-
/**
13-
* @ORM\Column(name="id", type="integer")
14-
* @ORM\Id
15-
* @ORM\GeneratedValue(strategy="AUTO")
16-
*/
10+
#[ORM\Id]
11+
#[ORM\GeneratedValue]
12+
#[ORM\Column(type: 'integer')]
1713
private $id;
1814

19-
/**
20-
* @ORM\Column(name="title", type="string", length=255)
21-
*/
15+
#[ORM\Column(type: 'string', length: 255)]
2216
private $title;
2317

2418
public function getId()
2519
{
2620
return $this->id;
2721
}
2822

29-
/**
30-
* @return mixed
31-
*/
32-
public function getTitle()
23+
public function getTitle(): string
3324
{
3425
return $this->title;
3526
}
3627

37-
/**
38-
* @param mixed $title
39-
*/
40-
public function setTitle($title)
28+
public function setTitle(string $title): self
4129
{
4230
$this->title = $title;
31+
32+
return $this;
4333
}
4434
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use App\Repository\SweetFoodRepository;
6+
use Doctrine\ORM\Mapping as ORM;
7+
8+
#[ORM\Entity(repositoryClass: SweetFoodRepository::class)]
9+
class SweetFood
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column(type: 'integer')]
14+
private $id;
15+
16+
#[ORM\Column(type: 'string', length: 255)]
17+
private $title;
18+
19+
public function getId()
20+
{
21+
return $this->id;
22+
}
23+
24+
public function getTitle(): string
25+
{
26+
return $this->title;
27+
}
28+
29+
public function setTitle(string $title): self
30+
{
31+
$this->title = $title;
32+
33+
return $this;
34+
}
35+
}

tests/fixtures/make-crud/SweetFoodRepository.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Doctrine\Persistence\ManagerRegistry;
88

99
/**
10+
* @extends ServiceEntityRepository<SweetFood>
11+
*
1012
* @method SweetFood|null find($id, $lockMode = null, $lockVersion = null)
1113
* @method SweetFood|null findOneBy(array $criteria, array $orderBy = null)
1214
* @method SweetFood[] findAll()
@@ -19,16 +21,21 @@ public function __construct(ManagerRegistry $registry)
1921
parent::__construct($registry, SweetFood::class);
2022
}
2123

22-
/*
23-
public function findBySomething($value)
24+
public function add(SweetFood $entity, bool $flush = false): void
2425
{
25-
return $this->createQueryBuilder('t')
26-
->where('t.something = :value')->setParameter('value', $value)
27-
->orderBy('t.id', 'ASC')
28-
->setMaxResults(10)
29-
->getQuery()
30-
->getResult()
31-
;
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+
}
3240
}
33-
*/
3441
}

0 commit comments

Comments
 (0)