【问题标题】:CakePHP: onError method in AppModel is not called when there is a sql errorCakePHP:当出现 sql 错误时,不调用 AppModel 中的 onError 方法
【发布时间】:2011-11-24 12:03:54
【问题描述】:

我在 AppModel 中添加了 onError 方法如下:

class AppModel extends Model {
    function onError() {
        $db = $this->getDataSource();
        $err = $db->lastError();
        $this->log("AppModel.onError: $err");
    }
}

在我的比赛控制器中,我有:

function admin_create() {
    $this->Match->query('SELECT bang FROM');
}

当我访问 /admin/controllers/create 时,它​​在日志文件中得到以下错误,但 onError 方法中的代码没有。

2011-11-24 20:50:43 警告:警告 (512):SQL 错误: 1064:您有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在行附近的 '' 附近使用正确的语法 1 英寸 [/home/larry/workspace/cbhk/cake/libs/model/datasources/dbo_source.php, 第684行]

另外,当我在 admin_create 中设置断点时,它已到达,但无法到达 onError 中的断点。有什么想法吗?

【问题讨论】:

    标签: php cakephp error-handling


    【解决方案1】:

    我不相信在使用 Model 的 query() 方法时会执行 onError 回调——只有在出现 SQL 错误时才会抛出异常。

    编辑:

    抱歉,我应该提供一个您如何处理错误的示例:

      try {
        $this->Match->query('SELECT bang FROM');
      } catch (Exception $e) {
      /**
       * Catch the exception - do your error handling here
       */
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多