⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Callback_after_insert spawning multiple row in table



wahyuhandy

wahyuhandy
  • profile picture
  • Member

Posted 11 July 2017 - 15:45 PM

I'm trying to log admin activity and generating the crud via helper, it's working great but there's one problem.

When it's logging the data after the user just *updating* one row, it spawning like 4-6 insert AND update log data to the log table.

Can anyone help me in this case?

 

Here is my code in the helper.

function render_event($table_name)
{
    $CI = & get_instance();  
    $crud = new grocery_CRUD();
    $crud->set_table($table_name);

    if ($CI->session->userdata('privileges') > 2) {
        $crud->unset_add();
        $crud->unset_print();
        $crud->unset_export();
        $crud->unset_edit();
        $crud->unset_delete();
    } elseif ($CI->session->userdata('privileges') > 1) {
        $crud->unset_delete();
        $crud->callback_after_insert(log_execution('insert', $table_name));
        $crud->callback_after_update(log_execution('update', $table_name));
    } else {
        $crud->callback_after_insert(log_execution('insert', $table_name));
        $crud->callback_after_update(log_execution('update', $table_name));
    }

    return $crud->render();
}

and the log_execution() in the same helper file

function log_execution($exec, $table_name)
{
    $CI = & get_instance();  
    $log_data = array(
        'uid' => $CI->session->userdata('uid'),
        'username' => $CI->session->userdata('username'),
        'execution' => $exec,
        'table_name' => $table_name,
        'date_execution' => date('Y-m-d H:i:s')
    );

    $CI->db->insert('operations_logs', $log_data);
}