← Back
Editing: ItemDeleteController.php
<?php namespace App\Controller\Admin\Tool; use App\Entity\Adresse; use App\Entity\Item; use App\Entity\Note; use App\Form\Admin\DeleteItemType; use App\Repository\AdresseRepository; use App\Repository\ItemRepository; use App\Repository\NoteRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class ItemDeleteController extends AbstractController { /** * @return Response */ #[Route(path: 'admin/delete-item', name: 'admin_delete_item')] public function index(Request $request, EntityManagerInterface $em) { $form = $this->createForm(DeleteItemType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->handleForm($form, $em); $this->addFlash('success', 'Items supprimés'); } return $this->render('admin/tool/item_delete.html.twig', ['deleteItemForm' => $form->createView()]); } private function handleForm(FormInterface $form, EntityManagerInterface $em): void { $data = $form->getData(); $items = []; /** @var AdresseRepository $adresseRepository */ $adresseRepository = $em->getRepository(Adresse::class); /** @var ItemRepository $itemRepository */ $itemRepository = $em->getRepository(Item::class); /** @var NoteRepository $noteRepository */ $noteRepository = $em->getRepository(Note::class); foreach ($data['item'] as $item) { $itemObject = $itemRepository->find($item); $items[] = $itemObject; $noteRepository->removeNoteWithItemForAgency($data['agency'], $itemObject); } $addresses = $adresseRepository->getByAgency($data['agency']->getId()); foreach ($addresses as $address) { foreach ($items as $item) { $address[0]->removeItem($item); } $em->persist($address[0]); } $em->flush(); } }
Save File
Cancel