试图调用名为 "remove" 的未定义方法 class

Attempted to call an undefined method named "remove" of class

我正在尝试制作 DELETE methode ( CRUD ) Symfony 4.2 ,如下所示:

/**
 * @Route("/delete/{id}", name="delete")
 * Method({"DELETE"})
 */
public function Delete(Request $request , $id)
{
    $etudiant = $this->getDoctrine()->getRepository(Etudiant::class)->find($id);

    $entityManger = $this->getDoctrine()->getManager();
    $entityManger = $this->remove($etudiant);
    $entityManger = $this->flush();
    $response = new Response();
    $response->send();

    return $this->redirectToRoute('home');
}

但是我的浏览器向我显示了这个错误:

Attempted to call an undefined method named "remove" of class;

多次赋值的部分有误。您必须调用实体管理器上的方法,而不是 $this.

    $entityManger = $this->getDoctrine()->getManager(); // get once, use as much as you need

    $etudiant = $entityManger->getRepository(Etudiant::class)->find($id);
    $entityManger->remove($etudiant);
    $entityManger->flush($etudiant); // only flush this entity, you never know what else is going on in listeners and such. You don't want to flush other entities, if you can avoid as easily as here.

    // these two lines make little sense. You should return a response, which is the redirect. No need to "send" anything else...
    //$response = new Response();
    //$response->send();

    return $this->redirectToRoute('home');