2016-01-21 49 views
1

我想創建一個刪除學生的按鈕。但我有一個問題,我的按鈕不會在我的控制器中啓動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> 

謝謝您的幫助。

+0

我想,你在路由中的'WCSCantineBundle:CanteenManager:delete'末尾忘了單詞「Action」。 –

+0

我會嘗試,但通常它的工作原理沒有這樣的路線: wcs_cantine_todayList: 路徑:/ todayList/{schoolId} 默認值:{_controller: 「WCSCantineBundle:CanteenManager:todayList」} 要啓動todayListAction –

+0

是的,你是對。 「Action」會自動添加(多麼奇怪的腳本語言)。你是否檢查過,如果刪除參數時調用了deleteAction方法? –

回答

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

你的代碼失敗,因爲教義實體管理器沒有關於$瞳就是因爲有實體管理的兩個不同的實例線索,上面的代碼是去除瞳孔手短,但可以根據需要改變它只要實體經理「知道」實體

+0

您可能是對的,但實際的代碼差異在哪裏?您將'$ this-> getDoctrine() - > getManager()'的結果保存到變量'em'中,以便稍後再次使用它。但是,那麼你就像OP一樣檢索「pupil」。 –

+0

OP創建實體管理器兩次,第一個知道$ pupil,第二個不知道,第二個調用flush(),因此remove函數對數據庫沒有影響 – Dheeraj

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} 我感謝大家的幫助。

相關問題