查询删除Symfony中的外键

问题描述:

有人可以帮我解决这个问题,创建一个查询,如果用户试图删除外键,它会给他们一个错误,而不是使用异常。查询删除Symfony中的外键

public function findByStudent($studentid, $id){ 
    return $this->getEntityManager() 
      ->createQuery(
       'select p from AcmeDemoBundle:Student 
        where studentid = :studentid AND id= :id 
        ') 
      ->setParameter('student',$studentid) 
      ->setParameter('id',$id)   


        ; 

} 

最新通报

$student= $em->getRepository('AcmeDemoBundle:Student')->findOneby($studentid, $courdeId, $LecturerId); 
     if($student){ 
      $this->addFlash('error','ERROR! You cannot delete this Student'); 
     } 

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

学生实体

<?php 

namespace Acme\DemoBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Student 
* 
* @ORM\Table() 
* @ORM\Entity 
*/ 
class Student 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="studentid", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 

private $studentid; 

/** 
* @var string 
* 
* @ORM\Column(name="name", type="string", length=255) 
*/ 
private $name; 

/** 
* @var string 
* 
* @ORM\Column(name="address", type="string", length=255) 
*/ 
private $address; 

/** 
* @var string 
* 
* @ORM\Column(name="Programme", type="string", length=255) 
*/ 
private $programme; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="Date of Birth", type="date") 
*/ 
private $dateOfBirth; 

/** 
* @var string 
* 
* @ORM\Column(name="Contact", type="string", length=255) 
*/ 
private $contact; 


/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set studentid 
* 
* @param integer $studentid 
* @return Student 
*/ 
public function setStudentid($studentid) 
{ 
    $this->studentid = $studentid; 

    return $this; 
} 

/** 
* Get studentid 
* 
* @return integer 
*/ 
public function getStudentid() 
{ 
    return $this->studentid; 
} 

/** 
* Set name 
* 
* @param string $name 
* @return Student 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

/** 
* Set address 
* 
* @param string $address 
* @return Student 
*/ 
public function setAddress($address) 
{ 
    $this->address = $address; 

    return $this; 
} 

/** 
* Get address 
* 
* @return string 
*/ 
public function getAddress() 
{ 
    return $this->address; 
} 

/** 
* Set programme 
* 
* @param string $programme 
* @return Student 
*/ 
public function setProgramme($programme) 
{ 
    $this->programme = $programme; 

    return $this; 
} 

/** 
* Get programme 
* 
* @return string 
*/ 
public function getProgramme() 
{ 
    return $this->programme; 
} 

/** 
* Set dateOfBirth 
* 
* @param \DateTime $dateOfBirth 
* @return Student 
*/ 
public function setDateOfBirth($dateOfBirth) 
{ 
    $this->dateOfBirth = $dateOfBirth; 

    return $this; 
} 

/** 
* Get dateOfBirth 
* 
* @return \DateTime 
*/ 
public function getDateOfBirth() 
{ 
    return $this->dateOfBirth; 
} 

/** 
* Set contact 
* 
* @param string $contact 
* @return Student 
*/ 
public function setContact($contact) 
{ 
    $this->contact = $contact; 

    return $this; 
} 

/** 
* Get contact 
* 
* @return string 
*/ 
public function getContact() 
{ 
    return $this->contact; 
} 

}

<entity name="AcmeDemoBundle\Entity\Course" table="Course" repository-class="AcmeDemoBundle\Entity\CourseRepository"> 
<indexes> 
    <index name="IDX_1B4F90669AD94696" columns="studentid"/> 
</indexes> 
<id name="id" type="integer" column="id"> 
    <generator strategy="IDENTITY"/> 
</id> 
<field name="name" type="string" column="string" nullable="false"/> 

<many-to-one field="studentid" target-entity="Student"> 
    <join-columns> 
    <join-column name="studentid" referenced-column-name="studentid"/> 
    </join-columns> 
</many-to-one> 

更新的自定义库

 public function findByStudentid($studentid, $id, tutorId) 
{ 
    return $this->getEntityManager() 
    ->createQuery('select s from AcmeDemoBundle:Student s 
        where s.studentid = :studentid AND s.id= :id or studentid = :studentid AND p.tutorId= :tutorId ') 
       ->setParameter('studentid',$studentid) 
      ->setParameter('id',$id)   
      ->setParameter('tutorId',$tutorId) 
      ->getResults(); 
    $student= $this->entityManager->getRepository('AcmeDemoBundle:Student')->findBy(['studentid' => $studentid]); 
    if ($student){ 
     return false; 
    } else { 
     return true; 
    } 
} 
+0

你是什么意思给他们一个错误?只需用try/catch包装你的函数调用,然后处理Exception就可以了。 –

+0

错误消息说他们不能删除学生,我试过try/catch语法,但被告知不要使用这样的方式 –

+0

@bujulloyd你的存储库方法接受两个参数,但你用三个参数调用它。 – jkucharovic

在你的学生资料库创建类似的东西:

public function isDeletable($student) 
{ 
    //your conditions here 
    $courses = $this->entityManager->getRepository('Courses')->findBy(['student' => $student]); 
    $tutor = $this->entityManager->getRepository('Tutors')->findBy(['student' => $student]); 
    if ($courses || $tutor){ 
     return false; 
    } else { 
     return true; 
    } 
} 

和删除之前调用这个函数。如果它说true - 删除记录,如果false - 显示错误页面。

使用try catch语法

use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; 

try { 
    $em = $this->getDoctrine()->getManager(); 
    $student = $em->getRepository("AppBundle:Student")->findOneBy([ 
     'studentId' => $studentId, 
     'id' => $id 
    ]); 
    if(!$student) { ... your code ... } 

    $em->remove($student); 
    $em->flush(); // it there is exception connected with FK, then 
    // catch it below 
} catch(ForeignKeyConstraintViolationException $e) { 
    return $this->render(':error:page.html.twig',['error'=>$e]); 
} 

您还可以赶上

use Doctrine\DBAL\Exception\UniqueConstraintViolationException; 

和许多与数据库连接的其他异常。如果您只比较给定的$ studentId和$ id值,则不要在DQL中键入查询。方法findOneBy是最简单的解决方案。我建议在更复杂的查询中使用DQL。

如果我的答案似乎不清楚或者您想获得其他内容,请确定您的问题并通知我评论。

+0

我使用了一个尝试和捕捉语法之前,但被告知不使用这样的,我应该创建一个查询,如果学生ID匹配课程ID或讲师ID然后它不能被删除,因此一个错误将被给予 –

+0

因此,如果(!$学生)决定你的进一步逻辑和删除或不删除其他实体,你可以从没有DQL的存储库和指令中获得学生。 – Daniel

+0

我已经更新了我现在试过的问题 –