em = $entityManager; $metadata = $this->em->getClassMetadata($this->class); $this->class = $metadata->getName(); $this->repo = $this->em->getRepository($this->class); } public function indexAction(): Response { $repo = $this->em->getRepository($this->class); return $this->render('LcShopBundle:' . $this->template . ':index.html.twig', [ 'entities' => $repo->findAll(), 'routePrefix'=> $this->routePrefix ]); } public function showAction($id): Response { $enity = $this->repo->find($id); return $this->render('LcShopBundle:'.$this->template.':show.html.twig', [ 'entity' => $enity, 'routePrefix'=> $this->routePrefix ]); } public function editAction(Request $request, $id): Response { if ($id == 'new') { $entity = new $this->class; } else { $repo = $this->em->getRepository($this->class); $entity = $repo->find($id); } $form = $this->createForm($this->type, $entity); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->em->persist($entity); $this->getDoctrine()->getManager()->flush(); return $this->redirectToRoute($this->routePrefix . '_index'); } return $this->render('LcShopBundle:' . $this->template . ':edit.html.twig', [ 'entity' => $entity, 'form' => $form->createView(), 'routePrefix'=> $this->routePrefix ]); } public function deleteAction(Request $request, $id): Response { $enity = $this->repo->find($id); if ($this->isCsrfTokenValid('delete'.$enity->getId(), $request->request->get('_token'))) { $entityManager = $this->getDoctrine()->getManager(); $entityManager->remove($enity); $entityManager->flush(); } return $this->redirectToRoute($this->routePrefix.'_index'); } }