I just started learning symfony for the past couple of weeks and i've been trying to create a form to delete users from my database but the code that i've wrote doesnt work and i don't undertand what I've been doing wrong. if anyone see the issue thank you in advance.
/**
* @Route("/utilisateur/retirer", name="user_remove", methods={"GET","POST", "DELETE"})
* @return Response
**/
public function removeUser( ManagerRegistry $doctrine, EntityManagerInterface $em): Response
{
$form = $this->createForm(UserType::class);
$em = $doctrine->getManager();
$user = $doctrine->getRepository(User::class);
if ($form->isSubmitted() && $form->isValid()) {
$em->remove($user);
$em->flush();
}
return $this->render('back_office/user_remove.html.twig',[
'form' => $form->createView()
]);
}
My Answer:
"/delete/{id}", name="delete_user")_ */_ public function deleteUser($id, EntityManagerInterface $entityManager) {_ $user = $entityManager->getRepository(User::class)->find($id);_ if (!$user) {_ throw $this->createNotFoundException('User not found');_ }_ $entityManager->remove($user);_ $entityManager->flush();_ return $this->redirectToRoute('list_users');_ }
Rate this post
3 of 5 based on 7030 votesComments