簡體   English   中英

無法在codeIgniter中捕獲異常

[英]can't catch exception in codeIgniter

對不起,我的英語但是請我需要幫助,當我嘗試捕獲插入查詢時遇到問題

我有一個帶有3個主鍵的Employe表(矩陣,CIN,名稱)

當我添加具有重復值的新員工時,我想捕獲異常

我的模特

  function insert_Employe($donnees){
   try{
    $this->db->insert('Employe',$donnees);
    return 'employe enregistré avec succes';
   }catch(Exception $e)
   {
       return $e->getMessage();
   }
}

我的控制器

  function nouveau(){
    if(!$this->offres->_isLogged() || !$this->input->post('Ajax')==1){
        redirect(base_url().'users');
    }
    $values = $this->_get_values();
    try{
    $message = $this->employe->insert_Employe($values);
    echo $message;
    }catch(Exception $e){
        echo $e->getMessage();
    }
}

我無法捕捉到異常

這將永遠不會引發異常:

$this->db->insert('Employe',$donnees);

因此,您永遠不會捕捉到這樣的異常。 如果insert()方法失敗,並且在$db['default']['db_debug']設置為TRUE,則將生成一個自定義錯誤,否則將返回FALSE。

最好的選擇是在執行數據庫操作時檢查錯誤:

$error = $this->db->_error_message()
if(!empty($error)) {
  throw new Exception('Your message');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM