我遇到此問題,我只能得到錯誤消息。我有一些學生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'));
}
$ delete!= $ deletable – JimL
它實際上是可刪除而不是刪除。 –
您的'$ deletable'屬於'bool'類型。爲什麼?因此,您將無法''em-> remove()' –