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.
I tried to do with this method but without success. I edited the question the way I tried.
– Guilherme Rigotti
@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
– Vinicius Gabriel
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.
– Guilherme Rigotti
@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".
– Vinicius Gabriel
@Guilhermerigotti can inform me what returned your query?
– Vinicius Gabriel
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.
– Guilherme Rigotti
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.
– Vinicius Gabriel
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.
– Guilherme Rigotti
@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?
– Vinicius Gabriel
@Guilhermerigotti and changed his check to validate false in IF instead of true'?
– Vinicius Gabriel
Hell, I replicated yours and I didn’t even realize, I’m gonna try it here by changing.
– Guilherme Rigotti
I switched to
buildReturnObject
, but nothing has changed :(– Guilherme Rigotti
Dude, if I say you don’t believe me, it wasn’t working because I was in
development
and not inproduction
. But anyway I liked that way you passed me and I’ll start using. I marked as resolved.– Guilherme Rigotti
@Guilhermerigotti how good it worked!
– Vinicius Gabriel