2017-07-31 68 views
-1

有人可以幫我解決這個問題,創建一個查詢,如果用戶試圖刪除外鍵,它會給他們一個錯誤,而不是使用異常。查詢刪除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

回答

0

在你的學生資料庫創建類似的東西:

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 - 顯示錯誤頁面。

0

使用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

我已經更新了我現在試過的問題 –