Treat return with error in codeigniter

0

A call to the delete method is returning me the following error.

  

Error Number:

     
    

ERROR: update or delete on table "matricula" violates foreign key constraint "matricula_matricula_cobranca_fk" on table "movement_cobranca" DETAIL: Key (matricula) = (1908) is still referenced from table "          

DELETE FROM REGISTRATION WHERE REGISTRATION IN (1908)

         

Filename: models / Matricula_model.php

         

Line Number: 166

  

But I need to handle this error and display a message to the user only stating that it did not work.

In the controller I am just validating the input and calling in the Model.

Controller

public function excluir($matricula) {
   if ($this->matricula_model->excluir($matricula)->dbReturn === true)
     echo json_encode(['codigo' => 0]);
   else
     echo json_encode(['codigo' => 1]);
}

Model

function excluir($matricula)
{
    if ( $lista_matricula != "" ) {
        $sql_exclusao = "DELETE FROM matricula WHERE matricula IN (". $matricula .")";
        if (!$ret = $this->db->query($sql_exclusao))
        {
            return $this->buildReturnObject($this->db->error(),false);
        }
        return $this->buildReturnObject('',$ret);
    }
    return false;
}

I know why the error is occurring and I do not need to treat it at the moment (because it is referring to bd). What I need is a way to return a value for my view (I used booleans, but could be any other) to be able to display an alert, a message or whatever. I need my return to be true or false to pass to my view a code (0 or 1) and display a message according to it, however the way I did it is not working.

    
asked by anonymous 13.12.2018 / 18:07

3 answers

1

To handle bank errors you can create a new CI class with 'extends' CI_Model and create a function to generate an object with a successful return and / or error.

Example of the function in the new Class:

class CustomModel extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    protected function buildReturnObject($dbError,$dbReturn)
    {
        $retObj = new stdClass();
        $retObj->dbError = $dbError;
        $retObj->dbReturn = $dbReturn;
        return $retObj;
    }
}

And you can create treatments in the function of your Model denying the query on a IF check (it will be TRUE if an error occurs in the query), and if it happens you can return the object created with the error caused, if it does not happen you return the object with the successful return of the query, example of how it would look inside its function in the model:

if (!$ret = $this->db->query($sql, $binds))
{
    return $this->buildReturnObject($this->db->error(),false);
}
return $this->buildReturnObject('',$ret);

So when you get an unexpected result on the Controller you'll deal with what you want to do there. You can not forget that in order to use the new function created to manipulate the return object, any Model you create should extend its class CustomModel .

Remembering that in Controller, if you use this format, you always have to reference the position ->dbReturn after the end of the query, if something wrong this position will get false, and you can access the return of the error in ->dbError . The query will always follow this syntax:

$this->classe_model->getXXX($param)->dbReturn;
    
13.12.2018 / 20:12
0

Use Try Catch to handle exception.

    
13.12.2018 / 18:20
0

Try the following to see whether or not you succeeded in the model:

if(!$sql_exclusao){
    tratamento da excessão...
}
Or something like that. Where $ sql_exclusion is going to be bool if the query succeeded or not. I do not know if the environment you use allows, but php has this power to know whether the query returned well or not.

    
13.12.2018 / 18:25