有人可以幫我解決這個問題,創建一個查詢,如果用戶試圖刪除外鍵,它會給他們一個錯誤,而不是使用異常。查詢刪除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包裝你的函數調用,然後處理Exception就可以了。 –
錯誤消息說他們不能刪除學生,我試過try/catch語法,但被告知不要使用這樣的方式 –
@bujulloyd你的存儲庫方法接受兩個參數,但你用三個參數調用它。 – jkucharovic