Php Symfony中的外键问题

Php Symfony中的外键问题,php,symfony,Php,Symfony,我有这个问题,我只能得到错误消息。我有一些表,其中学生id是外键,但是即使你的id号不是任何一个表,它仍然会给出消息,你不能删除这个学生,但如果可以删除,就不会通过那里 public function findBystudentid($studentid) { $record= $this->getEntityManager()->getRepository('AcmeDemoBundle:record')->findBy(['studentid' =&g

我有这个问题,我只能得到错误消息。我有一些表,其中学生id是外键,但是即使你的id号不是任何一个表,它仍然会给出消息,你不能删除这个学生,但如果可以删除,就不会通过那里

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

    }
我认为您调用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() ...

首先,你的名字有点不对。您需要修复它,因为它往往有点混乱。考虑到这一点,我建议你这样做:

一,。检查学生是否可删除的控制器方法:

二,。控制器调用上述命令的操作


希望这对您有所帮助并澄清一下。

$delete!=$deletable实际上是deletable而不是delete。您的$deletable是bool类型。为什么?因此,您将无法$em->删除它存储库中会返回什么?@JovanPerovic,请您提出一个答案。是的,@user742736是正确的。从外观上看,findBystudentid属于您的控制器类,而不是您的存储库……我只是想找出表中是否存在学生id,然后返回true或FALSE控制器中的外观如何您的学生在其他地方被使用的可能性很小?您能分享问题所在吗?苏的其他人可以从你的错误中吸取教训:这是我的错误。停止播放$可删除
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;
}
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'));
}