单击按钮时为什么我的动作未加载?

时间:2016-01-21 19:35:16

标签: symfony controller onclick action

我想创建一个删除瞳孔的按钮。但我有一个问题,我的按钮没有在我的控制器中启动deleteAction。

我的控制器:

public function deleteAction(Eleve $id, $schoolId)
{
    $repository = $this->getDoctrine()->getManager()->getRepository('WCSCantineBundle:Lunch');
    $pupil = $repository->findOneBy(array(
        'eleve' => $id
    ));
    $em = $this->getDoctrine()->getManager();
    $em->remove($pupil);
    $em->flush();

    return $this->redirect($this->generateUrl('wcs_cantine_todayList', array('schoolId' => $schoolId)));
}

我的路线:

delete_pupil:
path:     /
defaults: { _controller: "WCSCantineBundle:CanteenManager:delete" }

我的按钮(在我看来):

<a href="{{ path('delete_pupil', { 'id': lunch.eleve.id, 'schoolId': ecole.id }) }}">Désinscrire</a>

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

public function deleteAction(Eleve $id, $schoolId)
{
    $em = $this->getDoctrine()->getManager();
    $pupil = $em->getRepository('WCSCantineBundle:Lunch')->findOneBy(array('eleve' => $id));

    $em->remove($pupil);
    $em->flush();

    return $this->redirect($this->generateUrl('wcs_cantine_todayList', array('schoolId' => $schoolId)));
}

你的代码失败了,因为教条实体管理器不知道$ pupil是什么,因为有两个不同的实体管理器实例,上面的代码是删除学生的简写,但你可以根据需要改变它,只要实体经理'知道'实体

答案 1 :(得分:0)

我做到了并且有效。

我的控制器:

public function deleteAction($id, $schoolId)
{
    $dateNow = new \DateTime();
    $em = $this->getDoctrine()->getManager();
    $lunches = $em->getRepository('WCSCantineBundle:Lunch')->findBy(array(
        'eleve' => $id,
        'date' => $dateNow
    ));
    foreach ($lunches as $lunch) {
        $em->remove($lunch);
    }
    $em->flush();

    return $this->redirect($this->generateUrl('wcs_cantine_todayList', array('schoolId' => $schoolId)));
}

我的路线:

delete_pupil:
path:     /todayList/{schoolId}/{id}
defaults: { _controller: "WCSCantineBundle:CanteenManager:delete" }
methods: [GET, DELETE]

我的观点:

<a href="{{ path('delete_pupil', {'id': lunch.eleve.id, 'schoolId': ecole.id }) }}">Désinscrire</a>

我错过了使用斜杠在我的路线中传递我的参数:/ todayList / {schoolId} / {id} 我感谢大家的帮助。