CodeIgniter Forums

Full Version: logging all user actions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am using CI 3.  I want to log all users insert, update, and delete to a specific table in my database.  Can I easily do this using MY_Model, or some other way?

Use the session to have the users id in it then you can get it in all your model methods,
create a method to do the users logging in your model then right after the method runs and
just before the return call your new user add method to save the logging to the database.
Yeah that is one option for sure. What about the $this-db->delete() or $this->db->query() function or something? Can I override that so I only write it once?

Is that file CI_DB_driver
I see this method which talks about "replication" not sure if this has other functionality it would induce.

I also see this method which allows you to override DB drivers.
You can make a method something like this:

PHP Code:
$data = [
    'the data you want to save' => '',

$query $this->db->get_where('tableName', ['id' => $id]);

if (
    return true;

    return $this->db->insert_id();

and pass in the parameters that you want to log for the user.
Yeah that would work pretty well. Do you think it would be best to override the DB driver simple_query method?
Hello just use a model that autoload, and have the function to set the logs, something like this:

On Logs_Model:

public function set_log($action = FALSE, $extra = $FALSE)

$diccionario = array (

1 => 'has loged in',
2 => 'has loged out'
3 => 'created'. $extra[0]. " " .$extra[1],

$datestring = '%Y-%m-%d %h-%i-%s %a';

$log = array (
'logs_date' => mdate($datestring, time()),
'logs_event' => $diccionario[$action],

$this->db->insert('logs', $log);

Supouse you will create an user, in your controller:



$extra = array(
'the user',

$this->logs_model->set_log(3, $extra);
Thanks for your suggestion.

If you have 100 models. That means you would repeatedly add this code to each model. This seems like bad coding practice and also just a lot of work for no reason. Why do it that way as opposed to just override the simple_query method itself?
It is just one model, you call it's function set_log, on the controllers, after the operations, it's similar to the observer pattern. So you won't reoeat the code all the time.
No you would add it to a BaseModel just like CI 3 MY_Modal.

You put everything in the BaseModel.
Pages: 1 2