Codeigniter foreign key constraint check

依然范特西╮ 提交于 2019-12-04 07:43:41
Dan F.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!