代码点火器钩子未捕获插入/更新查询


CodeIgniter hook not catching insert/update queries

我正在尝试使用post_systempost_controller钩子来捕获和记录数据库表中的所有插入和更新查询。但是,发生的情况是$queries = $CI->db->queries;语句似乎根本没有捕获任何插入或更新语句。它只捕获 SELECT 语句,即使在相应的视图/控制器中插入新数据或更新旧数据也是如此。

这是我的相关代码:

钩子.php

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// also tried with post_controller hook
$hook['post_system'] = array(
    'class' => 'Db_query_log', 
    'function' => 'log_db_queries', 
    'filename' => 'db_log.php', 
    'filepath' => 'hooks'        
);

钩子/db_log.php

    <?php
class Db_query_log {
    function __construct() {
        //nothing special    
    }
    function log_db_queries() {
        $CI = & get_instance();
        $queries = $CI->db->queries;
        foreach ($queries as $key => $query) {
            echo $query . "<br>"; 
        // all statements displayed are SELECT statements even for UPDATE and INSERT operations performed by controllers where data is actually changed 
        }
    }
}

这里的罪魁祸首可能是什么?我是否错过了什么,或者这个钩子只是忽略了插入/更新操作?

任何帮助将不胜感激,谢谢!

使用 post_controller 而不是 post_system

$hook['post_controller'] = array(
    'class' => 'Db_query_log', 
    'function' => 'log_db_queries', 
    'filename' => 'db_log.php', 
    'filepath' => 'hooks'        
);

要实现此目的,请打开任何帮助程序文件并将此代码放在底部

function log_que($sql) {
        $filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php';   
            $handle = fopen($filepath, "a+");
            fwrite($handle, $sql." 'n Execution Time: ".date("Y-m-d H:i:s")."'n'n");  
            fclose($handle);   
}

之后转到系统/数据库/DB_driver.php并找到名为"查询"的函数并将其下面的代码放在函数内部的顶部。

log_que($sql);'

然后,所有查询将保存在应用程序/日志文件夹内的文件中。

如果您只想保存特定查询,则可以放置 if 条件。喜欢

if(preg_match('/INSERT/',$sql)) {
 fwrite($handle, $sql." 'n Execution Time: ".date("Y-m-d H:i:s")."'n'n");  
}