Hook for Queries Log and Redirect Instruction

Asked

Viewed 42 times

-1

I’m using hook to log queries. However when I use redirect instead of load->view, the array db->queries is empty. I need to continue using the redirect statement. Can anyone help me?

I’m using this model:

$hook['post_controller'] = array(     // 'post_controller' indicated execution of hooks after controller is finished
    'class' => 'Db_log',             // Name of Class
    'function' => 'logQueries',     // Name of function to be executed in from Class
    'filename' => 'db_log.php',    // Name of the Hook file
    'filepath' => 'hooks'         // Name of folder where Hook file is stored
);
class Db_log {

var $CI;

function __construct() {
    $this->CI = & get_instance(); // Create instance of CI
}
function logQueries() {

    $filepath = APPPATH . 'logs/QueryLog-' . date('Y-m-d') . '.php'; // Filepath. File is created in logs folder with name QueryLog
    $handle = fopen($filepath, "a+"); // Open the file

    $times = $this->CI->db->query_times; // We get the array of execution time of each query that got executed by our application(controller)

    foreach ($this->CI->db->queries as $key => $query) { // Loop over all the queries  that are stored in db->queries array
        $sql = $query . " \n Tempo de execução:" . $times[$key]; // Write it along with the execution time
        fwrite($handle, $sql . "\n\n");
    } 
    fclose($handle); // Close the file
}

The controller, instead of using a $this->view("unimed") I’m using redirect(base_url().'unimed');

What I realized is that when using Redirect, it loses (or restarts) the entire controller.. and hence the array values $this->db->queries empty I need to continue using redirect for the following reason: when using a load->view() the url remains the last one passed. And with redirect the url comes clean ex: with

load->view("unimed") 

after saving a record

url comes: localhost/profit/manager/Unimed/write/? id=&par=

ex: with

redirect(base_url().'unimed')

after saving a record the url comes localhost/profit/manager/Unimed

  • You need to give more details. When this hook is excommunicated? could you put his code? and the function where is the redirect() ?

  • I’m using this model.

1 answer

1

[RESOLVED] I did it this way: I created a new item in config.php:

$config['gera_arquivo_log'] = TRUE and I changed the Db_driver including the following instruction:

    $ci = get_instance();
    if($ci->config->item('gera_arquivo_log')===TRUE and $this->save_queries === TRUE):
        Check::gravaFileLog($sql, $time_end - $time_start);
    endif;

I created a static method in my Check class :

public static function gravaFileLog($query, $time) {
    $filepath = APPPATH . 'logs/QueryLog-' . date('Y-m-d') . '.php'; // Filepath. File is created in logs folder with name QueryLog
    $handle = fopen($filepath, "a+"); // Open the file

    $sql = $query . " \n Tempo de execução:" . $time; // Write it along with the execution time
    fwrite($handle, $sql . "\n\n");
    fclose($handle); // Close the file
}

If anyone has a more appropriate solution I thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.