5

Let me first explain the table structure:

+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(255) | NO   |     | NULL    |                |
| name      | varchar(255) | NO   |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+

The id field is referenced from another table through a foreign key.

Say I have a function in my model like this:

public function delete_user($id) {
    return $this->db->delete('users', array('id' => $id));
}

When this user is referenced from the other table, it should throw an error. The "bad" thing is, that Codeigniter actually shows that error on a full page:

Error Number: 1451

Cannot delete or update a parent row: a foreign key constraint fails (`testing`.`users_link`, CONSTRAINT `users_link_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))

DELETE FROM `users` WHERE `id` = '1'

Is there a way to make the delete_user function return FALSE instead of showing the error? I'd like to notify the user that they have to do something else first. I tried using transactions and returning the transaction_status, but that still throws the error.

2
  • well you need to modify delete function Commented May 5, 2012 at 15:49
  • Found the answer myself: $db['default']['db_debug'] = FALSE; and the errors are not displayed anymore so I can show my custom error messages :) Commented May 5, 2012 at 15:49

2 Answers 2

8

in config folder database.php file find the line

$db['default']['db_debug']

and set it to FALSE. This will prevent Codeigniter from printing the error on the screen.

Also in the delete function you can check:

if ($this->db->_error_number() == 1451)

If you need to do something special when this happens.

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

2 Comments

Remeber that in Codeigniter 3.0 (CI3), this is now $errors = $this->db->error();
If you're looking for the $db['default']['db_debug'] line, it is located in /application/config/database.php if using default environment.
0

I use it in this way Codeigniter 3.0

Model

/*
 * function to delete user
 */
function delete_user($id)
{
            if ( ! $this->db->delete('users', array('id' => $id)))
            {
                            return $this->db->error();
            }
    return FALSE;
}

Controller

$fk_check = $this->User_model->delete_user($id);

if($fk_check) {
// Unable to delete record
// $fk_check is an array containing $fk_check['code'] & $fk_check['message']
exit();
}

// Here your record has been deleted

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.