Codeigniter处理模型和控制器中的错误

时间:2013-12-23 11:17:28

标签: php codeigniter exception-handling try-catch

我正在尝试处理Model中的错误,然后将它们传递给Controller,但是我无法在Controller中捕获错误,我确定我正在做一些错误,但我不知道在哪里。

[UPDATE] 修正了错误。

public function updateOpera($image_id, $title, $author, $year, $annotation)
{
    try 
    {
        $conn = $this->oracle_connect(); 
        $author = 50;
        $procedure = "pck_opere.updateOpera(:p_image_id, :p_title, :p_author, :p_year, :p_annotation);";
        $sql = oci_parse($conn, "DECLARE obj INTEGER; begin obj := {$procedure} end;");  
        oci_bind_by_name($sql, ':p_image_id', $image_id, -1, OCI_B_INT);
        oci_bind_by_name($sql, ':p_title', $title, -1, SQLT_CHR);
        oci_bind_by_name($sql, ':p_author', $author, -1, OCI_B_INT);         
        oci_bind_by_name($sql, ':p_year', $year, -1, OCI_B_INT);         
        oci_bind_by_name($sql, ':p_annotation', $annotation, -1, SQLT_CHR);         
        $ex = oci_execute($sql);
        if($ex == 1)
        {
            oci_commit();
            return true;
        }
        else
            throw new Exception('Update failed');           
    }
    catch (Exception $e) 
    { 
        throw new Exception($e->getMessage());
    }
    $this->oracle_close($conn);        
}

这里有控制器:

    public function edit_validation()
    {
        try
        {
            // CONTROLLO SE L'UTENTE E' LOGGATO
            if($this->session->userdata('logged_in') == FALSE )
                redirect(base_url().'index.php/account/login');
            else
            {
                if(isset($_POST['submit']))
                {
                    // RICEVO E SETTO VARIABILI
                    $image_id = $_POST['id'];
                    $title = $_POST['title'];
                    $author = $_POST['author'];
                    $year = $_POST['year'];
                    $annotation = $_POST['annotation'];
                    // CONTROLLO VALIDITA' VARIABILI
                    if($image_id != null && $title != null && $author != null && $year != null)
                    {
                        $this->load->model('opere_model');
                        $updateOpera = $this->opere_model->updateOpera($image_id, $title, $author, $year, $annotation);
                        if($updateOpera)
                            redirect(base_url().'index.php/opere/show/'.$image_id);
                    }
                    else
                        $this->load->view('edit_opera_view', $data['error'] = "Please fill out mandatory fields."); 
                }
                else
                    $this->load->view('edit_opera_view', $data['error'] = "An error occurred. Please try to perform your changes later.");                
            }
        }
        catch(Exception $e)
        {
            $data['error'] = $e->getMessage();
            // CARICO VISTE
            $this->load->view('header');
            $this->load->view('menu');   
            $this->load->view ('edit_opera_view', $data);                
        }
    }   

0 个答案:

没有答案