繁体   English   中英

Symfony中的外键问题

[英]Foreign Key issues in Symfony

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

    }

首先,您的命名有点偏离。 您需要对其进行修复,因为它可能会有些混乱。 考虑到这一点,我建议您这样做:

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

希望能有所帮助并澄清一下。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM