Welcome Guest, Not a member yet? Register   Sign In
Problems with foreign key
#1

[eluser]DarthVinsus[/eluser]
Hello people, how can I solve this problem with codeigniter?

Code:
A Database Error Occurred

Error Number: 1451

Cannot delete or update a parent row: a foreign key constraint fails (`censo_ap`.`usuario`, CONSTRAINT `Refrol20` FOREIGN KEY (`rol_id`) REFERENCES `rol` (`rol_id`))

DELETE FROM `rol` WHERE `rol_id` = '2'
#2

[eluser]DarthVinsus[/eluser]
I half solved the problem

Code:
if ($this->db->_error_message()){

            $msg = $this->db->_error_message();
            $num = $this->db->_error_number();

            $data['msj']= "Error(".$num.") ".$msg;
            $this->load->view('request-error',$data);

        }

only need to identify the error num and associate with a more clientside message
#3

[eluser]Dam1an[/eluser]
Do you want the delete to fail in this case, or do you want it to delete the constraint rows, and then delete this row?
#4

[eluser]DarthVinsus[/eluser]
I want the delete fail, and it fails, but I should give the client an understanding human-like message. I have solved it with a library

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Description of MyUtils
*
* @author David Kruger
*/
class MyUtils {

    private $CI;
    private $error;
    private $message;
    private $data = array();

    function MyUtils()
    {
        $this->CI =& get_instance();
        $this->CI->load->database();
    }

    // check if they have a error with the query
    function check_query()
    {
        // check if the database return a error
        if ($this->CI->db->_error_message()){

            $this->message = $this->CI->db->_error_message();
            $this->error = $this->CI->db->_error_number();

            $this->data['error'] = $this->error;
            $this->data['message'] = $this->message;

        }

        return $this->data;

    }

}
?>

and I can use it where I will.

Code:
$this->load->library('myutils');

// check if they have database error
        $res = $this->myutils->check_query();
        if ($res)
        {
            if($res['error'] == 1451)
                $message= 'Can not delete this item, it is used in another table';

            $tipo_mensaje = 'request-error';
        }

What I only need is a list of the error number and associate with a better human-like message
#5

[eluser]Dam1an[/eluser]
Instead of doing the check for what error type it is in the model (or where ever you use it) I would suggest setting it as a string in the library so you can then just do
Code:
$error = $this->myutils->error;

I'd also recommend using a switch which is faster then a whole series of if else statement (and having the more common errors first so you exit the lookup asap)

As for where you can get these... no idea, you tried Google?
#6

[eluser]Cro_Crx[/eluser]
Dam1an: I think in other programming languages switch-case statements are usually quicker than using elseif. Although in PHP they seem to perform almost exactly the same. http://www.phpbench.com/ (4th test from the top). In that particular test else if was a tad quicker.

Although putting the most common errors first is defiantly good design and speeds up the program a lil Big Grin
#7

[eluser]Dam1an[/eluser]
[quote author="Cro_Crx" date="1242425270"]Dam1an: I think in other programming languages switch-case statements are usually quicker than using elseif. Although in PHP they seem to perform almost exactly the same. http://www.phpbench.com/ (4th test from the top). In that particular test else if was a tad quicker.

Although putting the most common errors first is defiantly good design and speeds up the program a lil Big Grin[/quote]

Really?!?! *Looks at benchmarks*
I guess this is comming from a primarily Java background, and the difference there was much quicker. It might be to do with the fact in Java, a switch only works on numbers and chars (not strings) so they can be optimised more then a switch on strings could in PHP?
#8

[eluser]Cro_Crx[/eluser]
We were always taught to use switch case when doing C programming. That website is really good for optimizing your code but I'm always going to use foreach no matter how slow it is :S




Theme © iAndrew 2016 - Forum software by © MyBB