logging all user actions - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: logging all user actions (/showthread.php?tid=77436) Pages:
1
2
|
logging all user actions - Goddard - 09-01-2020 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 RE: logging all user actions - InsiteFX - 09-02-2020 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. RE: logging all user actions - Goddard - 09-02-2020 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 RE: logging all user actions - Goddard - 09-02-2020 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-68512-post-345512.html#pid345512 RE: logging all user actions - InsiteFX - 09-02-2020 You can make a method something like this: PHP Code: $data = [ and pass in the parameters that you want to log for the user. RE: logging all user actions - Goddard - 09-02-2020 Yeah that would work pretty well. Do you think it would be best to override the DB driver simple_query method? RE: logging all user actions - Omar Crespo - 09-02-2020 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); RE: logging all user actions - Goddard - 09-02-2020 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? RE: logging all user actions - Omar Crespo - 09-02-2020 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. RE: logging all user actions - InsiteFX - 09-02-2020 No you would add it to a BaseModel just like CI 3 MY_Modal. You put everything in the BaseModel. |