在 Symfony 中查询删除外键
Query to Delete foreign key in 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;
}
}
使用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。
如果我的回答好像不清楚,或者你还想得到别的东西,请把你的问题说清楚,在评论里告诉我。
在你的学生资料库中创建类似这样的东西:
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
- 显示错误页面。
谁能帮我解决这个问题,创建一个查询,如果用户试图删除外键,它会给他们一个错误,而不是使用异常。
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;
}
}
使用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。
如果我的回答好像不清楚,或者你还想得到别的东西,请把你的问题说清楚,在评论里告诉我。
在你的学生资料库中创建类似这样的东西:
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
- 显示错误页面。