我正在使用此模型代碼來刪除記錄。Yii完整性約束異常處理與用戶友好的消息
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
包含此記錄的表與具有刪除限制約束的其他表具有一對多關係。
所以刪除已在子表相關記錄記錄時,它拋出像
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`bzuexamsystem`.`campus`, CONSTRAINT `fk_Campus_City` FOREIGN KEY (`CityID`) REFERENCES `city` (`CityID`) ON UPDATE CASCADE). The SQL statement executed was: DELETE FROM `city` WHERE `city`.`CityID`=1
例外,有什麼方法來顯示用戶友好的錯誤消息。由於
你的意思就像try/catch? – Asgaroth
if($ this-> loadModel($ id) - > delete()){...} else {...} should be work –