2014-03-24 48 views
1

對不起,我的英語,但請我需要幫助 我有,當我試圖抓住一個插入查詢不能趕上例外笨

我有3個主鍵的表僱工問題(Matricule, 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(); 
    } 
} 

我不能捕獲該異常

+0

所以'$這個 - > DB->插入()'導致您在'insert_Employe(捕獲異常)'和你想知道爲什麼後者不會拋出異常? – kero

+0

當我插入重複值的異常處理 我想趕上這個異常通知用戶,他必須更改導致問題的重複值 – bouchnina

回答

2

這絕不會拋出異常:

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

所以你永遠不會趕上這樣的異常。如果insert()方法失敗,那麼如果$db['default']['db_debug']在database.config上設置爲TRUE,則會生成顯示自定義錯誤,否則僅返回FALSE。

最好的辦法是做你的數據庫操作時,檢查錯誤:

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