Symfony中的外键问题

时间:2017-09-28 20:14:45

标签: php symfony

我遇到了这个问题,我只能收到错误消息。我有一些表格,其中学生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'));

    }

2 个答案:

答案 0 :(得分: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'));
}

希望得到这个帮助并澄清一下。

答案 1 :(得分: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() ...