Search code examples
phpdatabaseformssymfonycrud

delete data from the database using symfony


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()
         ]);
    }

Solution

  • By the way you've done : your user variable is the user repository not the user entity

    You need to get user Entity.

    Either you can send the id of user than find the user like this :

     $user = $doctrine->getRepository(User::class)->find($request->get('id'))
    

    or you can pass the user id via parameter in url :

    url : "/utilisateur/retirer/{id}"

    parameters : User $user

    /**
         * @Route("/utilisateur/retirer/{id}", name="user_remove", methods={"GET","POST", "DELETE"})
         * @return Response
         **/
        public function removeUser(User $user, ManagerRegistry $doctrine, EntityManagerInterface $em): Response 
        {
            $form = $this->createForm(UserType::class);
            $em = $doctrine->getManager();
            if ($form->isSubmitted() && $form->isValid()) {
                $em->remove($user);
                $em->flush();        
            }
             return $this->render('back_office/user_remove.html.twig',[
                 'form' => $form->createView()
             ]);
        }
    

    In aim of making more secure you can try this code bellow :

    Controller :

     /**
         * @Route("/{id}", name="user_delete", methods={"POST"})
         */
        public function delete(Request $request, User $user): Response
        {
            if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($user);
                $entityManager->flush();
            }
    
            return $this->redirectToRoute('user_index', Response::HTTP_SEE_OTHER);
        }
    

    Form :

    <form method="post" class="position-absolute  ms-5" action="{{ path('user_delete', {'id': user.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
        <input type="hidden" name="_token" value="{{ csrf_token('delete' ~ user.id) }}">
        <button class="btn btn-danger" type="submit">Delete</button>
    </form>