Problems when I try to delete

edited June 2014 in Modules
Hi,

I have too many modules related to others. For example, the module portal users and the module company.

In the database, the table company has a foreign key to the users table.

If a try to delete a user, the portal throws database error and I understand why. I can't delete a user if other tables are related to him.

But my question is how can I do to display a friendly message error when this happen instead of

A Database Error Occurred Error Number: 1451 Cannot delete or update a parent row: a foreign key constraint fails...

I realized that the delete function in the module.php file use flags to handle the errors

// Flags $any_success = $any_failure = FALSE; foreach ($posted as $id) { if ($this->model->delete(array($this->model->key_field() => $id))) { $any_success = TRUE; } else { $any_failure = TRUE; } }

According to this, the database error shouldn't be displayed. Just a message should be displayed according to the flag.

How can I do to avoid the database error?

Comments

  • edited 11:00PM
    That database error you are seeing is actually different and is generated deeper inside of CI. I'd check out around line 1168 in the fuel/codeigniter/database/DB_Driver.php file. I think the only way to avoid it is to fix the database error.
  • edited 11:00PM
    Thanks, I didn't know about the sql options ON DELETE action. This got fixed setting CASCADE option. That was what I want.

    Before I discover this, I realized of the on_before_delete hook. I try to use it to delete some files when I'm going to delete a model with files, but It seems that the hook is not recognized. I even try to print some data but nothing was displayed.

    I'm using the hook in the model of the module, is that ok?
  • edited 11:00PM
    Can you display the code you are using for your model for the hook?
Sign In or Register to comment.