• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logging all user actions

#1
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?

Thanks
Reply

#2
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
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
Reply

#4
I see this method which talks about "replication" not sure if this has other functionality it would induce.
https://forum.codeigniter.com/thread-68484.html

I also see this method which allows you to override DB drivers.
https://forum.codeigniter.com/thread-685...#pid345512
Reply

#5
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 (
$query)
{
    $this->db->update('tableName'$data);
    return true;
}
else
{
    $this->db->insert('tableName'$data);

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


and pass in the parameters that you want to log for the user.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
Yeah that would work pretty well. Do you think it would be best to override the DB driver simple_query method?
Reply

#7
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_user'=>$this>session>userdata('username'),
'logs_event' => $diccionario[$action],
);

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

Supouse you will create an user, in your controller:

...

$this->user_model->insert();

$extra = array(
'the user',
$this->input->post('username'),
);

$this->logs_model->set_log(3, $extra);
Reply

#8
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?
Reply

#9
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.
Reply

#10
No you would add it to a BaseModel just like CI 3 MY_Modal.

You put everything in the BaseModel.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.