diff --git a/src/Resources/skeleton/crud/controller/Controller.tpl.php b/src/Resources/skeleton/crud/controller/Controller.tpl.php index 3cf422598..ebc87499b 100644 --- a/src/Resources/skeleton/crud/controller/Controller.tpl.php +++ b/src/Resources/skeleton/crud/controller/Controller.tpl.php @@ -6,8 +6,9 @@ use ; use ; - + use Doctrine\ORM\EntityManagerInterface; + use Symfony\Bundle\FrameworkBundle\Controller\; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -44,18 +45,29 @@ public function index(EntityManagerInterface $entityManager): Response generateRouteForControllerMethod('/new', sprintf('%s_new', $route_name), ['GET', 'POST']) ?> +repositoryHasAddRemoveMethods($repository_full_class_name)) { ?> + public function new(Request $request, $): Response + public function new(Request $request, EntityManagerInterface $entityManager): Response + { $ = new (); $form = $this->createForm(::class, $); $form->handleRequest($request); +repositoryHasAddRemoveMethods($repository_full_class_name)) { ?> + if ($form->isSubmitted() && $form->isValid()) { + $->add($); + return $this->redirectToRoute('_index', [], Response::HTTP_SEE_OTHER); + } + if ($form->isSubmitted() && $form->isValid()) { $entityManager->persist($); $entityManager->flush(); return $this->redirectToRoute('_index', [], Response::HTTP_SEE_OTHER); } + return $this->renderForm('/new.html.twig', [ @@ -79,16 +91,27 @@ public function show( $): Re } generateRouteForControllerMethod(sprintf('/{%s}/edit', $entity_identifier), sprintf('%s_edit', $route_name), ['GET', 'POST']) ?> +repositoryHasAddRemoveMethods($repository_full_class_name)) { ?> + public function edit(Request $request, $, $): Response + public function edit(Request $request, $, EntityManagerInterface $entityManager): Response + { $form = $this->createForm(::class, $); $form->handleRequest($request); +repositoryHasAddRemoveMethods($repository_full_class_name)) { ?> + if ($form->isSubmitted() && $form->isValid()) { + $->add($); + return $this->redirectToRoute('_index', [], Response::HTTP_SEE_OTHER); + } + if ($form->isSubmitted() && $form->isValid()) { $entityManager->flush(); return $this->redirectToRoute('_index', [], Response::HTTP_SEE_OTHER); } + return $this->renderForm('/edit.html.twig', [ @@ -104,12 +127,22 @@ public function edit(Request $request, $generateRouteForControllerMethod(sprintf('/{%s}', $entity_identifier), sprintf('%s_delete', $route_name), ['POST']) ?> +repositoryHasAddRemoveMethods($repository_full_class_name)) { ?> + public function delete(Request $request, $, $): Response + public function delete(Request $request, $, EntityManagerInterface $entityManager): Response + { +repositoryHasAddRemoveMethods($repository_full_class_name)) { ?> + if ($this->isCsrfTokenValid('delete'.$->get(), $request->request->get('_token'))) { + $->remove($); + } + if ($this->isCsrfTokenValid('delete'.$->get(), $request->request->get('_token'))) { $entityManager->remove($); $entityManager->flush(); } + return $this->redirectToRoute('_index', [], Response::HTTP_SEE_OTHER); } diff --git a/src/Resources/skeleton/doctrine/Repository.tpl.php b/src/Resources/skeleton/doctrine/Repository.tpl.php index e540f5d9d..f06885cf6 100644 --- a/src/Resources/skeleton/doctrine/Repository.tpl.php +++ b/src/Resources/skeleton/doctrine/Repository.tpl.php @@ -4,6 +4,8 @@ use ; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\ORMException; use ; getFullName(), 'Password')) ? sprintf("use %s;\n", $password_upgrade_user_interface->getFullName()) : null ?> @@ -22,6 +24,30 @@ public function __construct(ManagerRegistry $registry) { parent::__construct($registry, ::class); } + + /** + * @throws ORMException + * @throws OptimisticLockException + */ + public function add( $entity, bool $flush = true): void + { + $this->_em->persist($entity); + if ($flush) { + $this->_em->flush(); + } + } + + /** + * @throws ORMException + * @throws OptimisticLockException + */ + public function remove( $entity, bool $flush = true): void + { + $this->_em->remove($entity); + if ($flush) { + $this->_em->flush(); + } + } diff --git a/src/Util/TemplateComponentGenerator.php b/src/Util/TemplateComponentGenerator.php index b55f744ee..61c74fda5 100644 --- a/src/Util/TemplateComponentGenerator.php +++ b/src/Util/TemplateComponentGenerator.php @@ -11,6 +11,9 @@ namespace Symfony\Bundle\MakerBundle\Util; +use ReflectionClass; +use ReflectionException; + /** * @author Jesse Rushlow * @@ -96,4 +99,14 @@ public function getPropertyType(ClassNameDetails $classNameDetails): ?string return sprintf('%s ', $classNameDetails->getShortName()); } + + /** + * @throws ReflectionException + */ + public function repositoryHasAddRemoveMethods(string $repositoryFullClassName): bool + { + $reflectedComponents = new ReflectionClass($repositoryFullClassName); + + return $reflectedComponents->hasMethod('add') && $reflectedComponents->hasMethod('remove'); + } } diff --git a/tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php b/tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php index 1e33c7692..66414ec39 100644 --- a/tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php +++ b/tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php @@ -4,6 +4,8 @@ use Symfony\Bundle\MakerBundle\Tests\tmp\current_project_xml\src\Entity\UserXml; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -19,6 +21,30 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, UserXml::class); } + /** + * @throws ORMException + * @throws OptimisticLockException + */ + public function add(UserXml $entity, bool $flush = true): void + { + $this->_em->persist($entity); + if ($flush) { + $this->_em->flush(); + } + } + + /** + * @throws ORMException + * @throws OptimisticLockException + */ + public function remove(UserXml $entity, bool $flush = true): void + { + $this->_em->remove($entity); + if ($flush) { + $this->_em->flush(); + } + } + // /** // * @return UserXml[] Returns an array of UserXml objects // */ diff --git a/tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php b/tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php index b09936382..2b7115ee3 100644 --- a/tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php +++ b/tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php @@ -4,6 +4,8 @@ use Symfony\Bundle\MakerBundle\Tests\tmp\current_project_xml\src\Entity\XOther; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -19,6 +21,30 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, XOther::class); } + /** + * @throws ORMException + * @throws OptimisticLockException + */ + public function add(XOther $entity, bool $flush = true): void + { + $this->_em->persist($entity); + if ($flush) { + $this->_em->flush(); + } + } + + /** + * @throws ORMException + * @throws OptimisticLockException + */ + public function remove(XOther $entity, bool $flush = true): void + { + $this->_em->remove($entity); + if ($flush) { + $this->_em->flush(); + } + } + // /** // * @return XOther[] Returns an array of XOther objects // */