学无先后,达者为师

网站首页 编程语言 正文

TP6记录报错的SQL语句

作者:懒是一种态度 更新时间: 2022-02-06 编程语言

在使用 ThinkPHP6 时,你会发现,即使开启了记录SQL,在执行到一句错误的SQL查询时,并没有跟踪到错误的SQL记录。在日志中只有这么一条前后不搭的报错:

[2021-12-26T21:29:44+08:00][error] [10501]SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distict' in 'field list'

这很摸不到头脑,于是改进一下,至少知道运行的SQL是神马玩意。

修改一下 vendor/topthink/think-orm/src/db/PDOConnection.php 第 795 行左右位置

修改前:

            if ($e instanceof \PDOException) {
                throw new PDOException($e, $this->config, $this->getLastsql());
            } else {
                throw $e;
            }

修改后:

            if ($e instanceof \PDOException) {
                $this->db->log( $this->getLastsql() .' => '. $e->getMessage() , 'error');
                throw new PDOException($e, $this->config, $this->getLastsql());
            } else {
                throw $e;
            }

这里就增加了一行日志记录($this->db->log(xxxxxx)),把错误语句和错误信息都记录下来。

当然也可以增加 debug_backtrace() 函数把错误跟踪也写进去,但thinkPHP6的错误跟踪实在是太多了,还是简单点好,知道哪里错了就行了。

 

原文链接:https://blog.csdn.net/shengshuai/article/details/122161349

栏目分类
最近更新