Symfony 启用查找和删除项目

Symfony enable to find and delete item

首先,这是我的代码:

   /**
     *
     * @Route("/delete", name="team_delete_v4")
     */
    public function deleteTeam(Request $request, BaseEquipe $team, $id)
    {
        $entityManager = $this->getDoctrine()->getManager();

        $repository = $this->getDoctrine()->getRepository(BaseEquipe::class);
        $equipe = $repository->find($team->getId());
        $entityManager->remove($equipe);


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

我的控制器中有一个函数,我尝试通过他的 ID get/find 一个对象并删除该对象。出于某种原因,Symfony 告诉我 "Unable to guess how to get a Doctrine instance from the request information for parameter "team"." 我猜不出如何解决它。你们能帮帮我吗?

PS : 我的版本其实是3.4

非常感谢

在你的控制器中你得到 3 个参数,$request,$team 和一个 id,但是 symfony 如何知道 $team 你发送到你的控制器。您必须在 url:

中传递 id
    /**
     * @Route("/delete/{id}", name="team_delete_v4")
     */
    public function deleteTeam(Request $request, BaseEquipe $team)
    {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->remove($team);


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

注意 url 现在是 /delete/{id},当你在控制器中声明一个 id 时,symfony 会找到这个 id 的对象。 Take a look at the docs.

因此在您的模板中,您必须传递要删除的对象的 ID:

path("team_delete_v4", {id: team.id})

抱歉我的英语不好,希望对您有所帮助。