Treat error return in codeigniter

Asked

Viewed 933 times

0

A call to the delete method returns me the following error.

Error Number:

ERROR: update or delete on table "matricula" violates Foreign key Constraint "matricula_matricula_cobranca_fk" on table "movimento_cobranca" DETAIL: Key (matricula)=(1908) is still referenced from table "movimento_cobranca".

DELETE FROM matricula WHERE matricula IN (1908)

Filename: models/Matricula_model.php

Line Number: 166


However I need to treat this error and display a message to the user just stating that it did not work.

In the controller I’m 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 don’t need to treat it at the moment (because it refers to the comic). What I need is a way to return a value to my view (I used booleans, but it could be anyone else) 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 according to it a message, but the way I did not work.

3 answers

1


For handling bank errors you can create a new class CI core with Ci_model extends and create a function to generate an object with the return of success 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 (error) in a check IF (will be TRUE if any error occurs in the query), and if it occurs you can return the created object with the occasional error, if it does not occur you return the object with the query successfully return, 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 receive an unexpected result in the Controller you’ll be there to see what you want to do. Do not forget that in order to use the new function created to manipulate the return object, every Model you create should extend its class CustomModel.

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

$this->classe_model->getXXX($param)->dbReturn;
  • I tried to do with this method but without success. I edited the question the way I tried.

  • @Would Guilhermerigotti have a way of showing how you tried? Because I use this way and there is no error, whenever there is error in the bank will be returned false to the Controller. We can go in chat if you want

  • I edited the question the way I tried, take a look to see if you understand, if we can’t go on chat yes without problems.

  • @Guilhermerigotti just to confirm, you created the new class in the IC Core and put it extending your Model? And also in this format, always take as a party check if dbReturn is false and play on Else True if applicable. For sure every error will be returned False, but not always that it works the return will be a "true".

  • @Guilhermerigotti can inform me what returned your query?

  • I put it inside the Ci_model, why not to extend more than 1 class, and the CI model needs to be extended. (correct me if I’m wrong) , the return ta being the same error that I put as quote in the question.

  • Yes, you really need to extend the Ci_model, but that’s why in the new Class you should extend it with the Ci_model, so everyone who is extended by their new class will also have the Ci_model, I didn’t get to test with the direct function in Ci_model so I can’t confirm if it really works, I can test.

  • I tried it out with her on the Ci_model to avoid the trouble of creating a new class, and apparently it didn’t work out the way I did, if I can test it we already take the test. if it works I was going to create a new class for her and then I organized it right.

  • @Guilhermerigotti pardon the delay. I tested with the Ci_model and it worked yes. I saw that in the code of my post has an error, I wrote the function as 'buildReturnObject' and call 'buildReturnObj', should be the same name, by chance you did not end up replicating it?

  • @Guilhermerigotti and changed his check to validate false in IF instead of true'?

  • Hell, I replicated yours and I didn’t even realize, I’m gonna try it here by changing.

  • I switched to buildReturnObject, but nothing has changed :(

  • 1

    Dude, if I say you don’t believe me, it wasn’t working because I was in development and not in production. But anyway I liked that way you passed me and I’ll start using. I marked as resolved.

  • @Guilhermerigotti how good it worked!

Show 9 more comments

0

Use Try Catch to treat exception.

  • I had already tried to use a Try/Catch block, but without success

  • 1

    try that way $this->db->simple_query('SELECT example_field FROM example_table'); $error = $this->db->error(); // Has Keys 'code' and 'message'

  • With simple_query it works, but js doesn’t get the value.

0

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

if(!$sql_exclusao){
    tratamento da excessão...
}

Or something like that. Where $sql_delete will be bool whether or not the query worked. I don’t know if the environment you use allows it, but php has this power to know if the query returned well or not.

  • Without success, only appears the error I put in the post and nothing true or false.

  • Have you tried this PHP manual on exceptions? It has some good examples: http://php.net/manual/en/language.exceptions.php

  • I have seen yes, but without success. The return is always the error and not a true or false for me to handle in the controller and display to the user

Browser other questions tagged

You are not signed in. Login or sign up in order to post.