简体   繁体   中英

Symfony - can't delete record

I am testing delete action in Postman. I created a delete action and it keeps returning 'success' and when I refresh the database my row with forwarded parameter(of id = 1) is still there.

My Service.

    /**
 * Delete user
 * @param $id
 * @return mixed
 */
public function deleteUser($id)
{
    $query = $this->getUserRepository()
        ->createQueryBuilder('du')
        ->delete('ProjectBundle:User','du')
        ->where('du.id = :id')
        ->setParameter("id", $id)
        ->getQuery()
        ->execute();

    return $query;

}

Controller.

/**
 * @Route("/users/delete/{id}", name="user_delete")
 * @param $id
 * @return \Symfony\Component\HttpFoundation\JsonResponse
 * @throws \Doctrine\Common\Annotations\AnnotationException
 */
public function getUserDeleteAction($id)
{
    $this->get('user')->deleteUser($id);

    return $this->success();
}

I think this is the right way and don't know where the problem could be.

You must use the ->remove() method, like this:

#controller
public function getUserDeleteAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository('AppBundle:User')->find($id);
    $em->remove($user);

    return $this->success();
}

Info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM