⚠ 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

Problem auditing queries in Codeigniter with Grocerycrud



Pedro Bernal
  • profile picture
  • Member

Posted 24 September 2014 - 15:45 PM

I need to audit the insert, edit and delete queries from my application, I tried using a hook that calls a model that save the query into a table in my database, however it is just saving the SHOW, DESCRIBE and SELECT statements, but the update, adding and deleting actions that I perform on the GroceryCrud views are not being saved.

Can someone help me to figure it out why, Thanks!

Hook code:

<?php
class Db_log {


    function __construct() 
    {
    }


    function logQueries() 
    {
        $CI = & get_instance();
        $CI->load->model("model_log");
        $filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php'; // Creating Query Log file with today's date in application/logs folder
        $handle = fopen($filepath, "a+");                 // Opening file with pointer at the end of the file
        $times = $CI->db->query_times;                   // Get execution time of all the queries executed by controller
        foreach ($CI->db->queries as $key => $query) 
        { 
            $sql = $query . " \n Execution Time:" . $times[$key]; // Generating SQL file alongwith execution time
            fwrite($handle, $sql . "\n\n");              // Writing it in the log file
            $CI->model_log->log($sql);                     // Calling the model
        }
        fclose($handle);      // Close the file
    } 
}


?>

 

 

Model code:

<?php
Class Model_log extends CI_Model
{
 public function log($sql)
  {
    if($_SESSION<>null)
    {
      $this-> db ->select('*');
      $this-> db ->from('usuarios');
      $this -> db -> where('nombre', $_SESSION["usuario"]);
      $query = $this->db->get();
      $user_data= get_object_vars($query->row());
      $sql = str_replace("'", "", $sql);
      date_default_timezone_set('America/Bogota');
      $date = date('Y-m-d H:i:s', time());
      $this->db->query("
          INSERT INTO `log`(`query`, `usuario`, `fecha`)
           VALUES
            ('".$sql."',".$user_data["id"].",'".$date."')");
    }
  }
}
?>
 
  

 

 


Amit Shah
  • profile picture
  • Member

Posted 25 September 2014 - 03:48 AM

i am sorry ... dont know how you achiving it .. but i have much simpler way (of course u can call it a hack) ... is to hook to the grocery_crud_model.php in that .. where ever the queries are getting executed.. post that u can just add logquery($this->db->last_query()) ... make sure u write the logquery function in helper and include it in autoload.php so it is available globally.

 

Hope this works out for u

 

Happy Hacking GC :)


Lucas
  • profile picture
  • Member

Posted 30 April 2015 - 19:23 PM

anyone have a different answer ?


Nitts S Chand
  • profile picture
  • Member

Posted 13 June 2016 - 12:16 PM

I have to perform to click delete icon in crud table to perform update operation and database table IS_DELETED =YES change flag. Any solution to replace delete query to Update Query....


Nitts S Chand
  • profile picture
  • Member

Posted 13 June 2016 - 12:20 PM

any one to solve  Above problem...! :-