I am experiencing some mistakes with my symfony crud system. Is there anyone that can help me out with it.
Read statement this is how I am getting the data from my database:
#[Route('/admin', name: 'app_admin')]
public function index(EntityManagerInterface $em): Response
{
$product = $em->getRepository(Product::class)->findAll();
return $this->render('admin/index.html.twig', [
'product' => $product,
])
This is the delete statement that I am using, but its not working how it should:
#[Route('/delete/{id}', name: 'delete')]
public function delete(EntityManagerInterface $em, int $id): Response
{
$product = $em->getRepository(Product::class)->find($id);
$em->remove($product);
$em->flush();
$name = $product->getName();
$this->addFlash('succes', "$name is verwijderd");
return $this->redirectToRoute('app_admin');
}
This is the update statement that I am using, but it is also not really working how it is supposed to be working:
#[Route('/update/{id}', name: 'update')]
public function update(EntityManagerInterface $em, int $id, Request $request): Response
{
$product = $em->getRepository(Product::class)->find($id);
$form = $this->createForm(UpdateType::class, $product);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$product = $form->getData();
$em->persist($product);
$em->flush();
$name = $product->getName();
$this->addFlash('succes', "Product $name is aangepast");
return $this->redirectToRoute('app_admin');
}
return $this->render('admin/update.html.twig', [
'form' => $form,
]);
}
This is the create statement this is working but not correctly:
#[Route('/insert', name: 'insert')]
public function insert(EntityManagerInterface $em, Request $request): Response
{
$product = $em->getRepository(Product::class)->find($id);
$form = $this->createForm(UpdateType::class, $product);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$product = $form->getData();
$em->persist($product);
$em->flush();
$name = $product->getName();
$this->addFlash('succes', "Product $name is aangepast");
return $this->redirectToRoute('app_admin');
}
return $this->render('admin/update.html.twig', [
'form' => $form,
]);
}
I tried checking the symfony website but it didn’t help. Is there anyone who can help me out?
vitalseal20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.