0

Inside a controller I want to make an insert into the database :

public function insertAction() {

        try {
            $this->load->model('action');
            $_POST['laDate'] = $this->input->post('ladate_') == '' ? '' : $this->dates->convertDateToFormatBase($this->input->post('ladate_'), '');
            $_POST['dateCreation'] = "now()";
            $action_id = $this->action->ajouter($_POST);
            if (isset($_POST['verificateurs'])) {
                $this->load->model('actionVerificateur');
                $this->actionVerificateur->inserer($action_id, $_POST['verificateurs']);
            }
            $this->load->view('ajax/insertAction', array('action_id' => $action_id));
        }
        catch (Exception $e) {
            $this->load->view('ajax/insertAction', array('action_id' => 0));
        }

    }

There is a unique constraint on some columns of the database table. So when this constraint is violated then I want to catch the exception. But it seems that my code does not catch it ! So how to catch the exception ?

3
  • what if you use Transactions. I never heard of using TRY CATCH in the framework. Cz Framework helps thing easy. But you doing old still Commented Aug 16, 2017 at 8:03
  • yeah , old school here lol :) Commented Aug 16, 2017 at 8:06
  • 1
    Check this out, this helped me earlier. Hope it will be helpful to you also. stackoverflow.com/questions/15858372/… Commented Aug 16, 2017 at 8:09

1 Answer 1

0

I found a workaround : I tested if $this->db->insert_id() is numeric ! because in the case an exception is raised then the returned value is an html code beggining with <DOCTYPE>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.