2017-09-28 69 views
0

我遇到此問題,我只能得到錯誤消息。我有一些學生ID是外鍵的表,但即使你的ID號碼不是任何表格,它仍然給出信息「你不能刪除這個學生」,但如果它可以刪除將不會通過那裏Symfony中的外鍵問題

public function findBystudentid($studentid) 
    { 


    $record= $this->getEntityManager()->getRepository('AcmeDemoBundle:record')->findBy(['studentid' => $studentid]); 
      $lecture = $this->getEntityManager()->getRepository('AcmeDemoBundle:lecture')->findBy(['studentid' => $studentid]); 
      $faculty = $this->getEntityManager()->getRepository('AcmeDemoBundle:faculty')->findBy(['studentid' => $studentid]); 
      if ($record||$lecture||$faculty){ 
         return true; 
        } else { 
         return false; 
        } 
} 

public function deleteAction(Request $request, $studentid) 
    { 
     $form = $this->createDeleteForm($studentid); 
     $form->handleRequest($request); 

     $em = $this->getDoctrine()->getManager(); 
     $deletable = $em->getRepository('AcmeDemoBundle:Student')->findBystudentid($studentid); 

      if ($deletable) { 
       $this->addFlash('error','ERROR! You cannot delete this Student'); 
      } 
      else 
       { 

       $em->remove($deletable); 
       $em->flush(); 
       $this->addFlash('error','Student Deleted'); 
       } 
      return $this->redirect($this->generateUrl('Student')); 

    } 
+0

$ delete!= $ deletable – JimL

+0

它實際上是可刪除而不是刪除。 –

+0

您的'$ deletable'屬於'bool'類型。爲什麼?因此,您將無法''em-> remove()' –

回答

1

首先,你的命名是有點過。你需要修復它,因爲它往往有點混亂。考慮到這一點,我建議你做這樣的:

1.控制器的方法來檢查,如果學生是可刪除的:

private function isStudentDeletable($studentid) 
{ 
    $em = $this->getEntityManager(); 

    $record= $em->getRepository('AcmeDemoBundle:record')->findBy(['studentid' => $studentid]); 
    if ($record){ 
     return false; 
    } 

    $lecture = $em->getRepository('AcmeDemoBundle:lecture')->findBy(['studentid' => $studentid]); 

    if ($lecture){ 
     return false; 
    } 

    $faculty = $em->getRepository('AcmeDemoBundle:faculty')->findBy(['studentid' => $studentid]); 

    if ($faculty){ 
     return false; 
    } 

    return true; 
} 

2.控制器的動作來調用上述

public function deleteAction(Request $request, $studentid) 
{ 
    $form = $this->createDeleteForm($studentid); 
    $form->handleRequest($request); 

    $deletable = $this->isStudentDeletable($studentid); 

    if (!$deletable) { 
     $this->addFlash('error','ERROR! You cannot delete this Student'); 
    } 
    else 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $student = $em->getRepository('AcmeDemoBundle:Student')->find($studentid) 
     $em->remove($student); 
     $em->flush(); 
     $this->addFlash('error','Student Deleted'); 
    } 

    return $this->redirect($this->generateUrl('Student')); 
} 

希望這個幫助和澄清一點。

+0

你的學生在其他地方有被遠程使用的機會嗎? –

+0

發生了什麼事?你有沒有設法讓它運行? –

+0

是的,我做了,謝謝你的回答 –

0

我認爲你打電話findBystudentid錯誤,因爲findBystudentid不在實體中。

這裏是更新的版本

public function deleteAction(Request $request, $studentid) 
{ 
    $form = $this->createDeleteForm($studentid); 
    $form->handleRequest($request); 

    $em = $this->getDoctrine()->getManager(); 
    $deletable = $this->findBystudentid($studentid); 

    if ($deletable) { 
     $this->addFlash('error','ERROR! You cannot delete this Student'); 
    } else { 
     $em->getRepository('AcmeDemoBundle:Student')->findBy(['studentid' => $studentid]) 
     $em->remove($deletable); 
     $em->flush(); 
     $this->addFlash('error','Student Deleted'); 
    } 

    return $this->redirect($this->generateUrl('Student')); 
} 

而且findBystudentid應該是一個私有函數

private function findByStudentId() ... 
+0

獲取嘗試調用方法的錯誤 –

+0

是的,@ user742736是正確的。從外觀上看,'findBystudentid'屬於你的'Controller'類,而不是你的倉庫... –

+0

我只是想找到學生ID是否存在於表中,然後返回true或false –