【问题标题】:Cakephp 3 get last query executedCakephp 3 获取执行的最后一个查询
【发布时间】:2015-12-01 21:17:18
【问题描述】:

在查询数据库时,我一直在尝试处理 cakephp 3 上的异常。

我想在触发异常时执行最后一个查询,以通过电子邮件通知管理员,我使用的是 MySQL 数据库,所以如果可能的话,错误代码也很高兴。

这是我现在的代码。

if($this->request->is('post')){
  $opciones=$this->request->data;

  $configsTable = TableRegistry::get('Configs');      

  try{        

    $configsTable->connection()->transactional(function() use($configsTable, $opciones){
      foreach ($opciones as $llave => $opcion) {
        $q = $configsTable->find('all', [
          'conditions' => [
            'Configs.nombre' => $llave
          ]
        ]);
        $reg = $q->first();

        if (empty($reg)) {
          $data = array();
          $data['nombre'] = $llave;
          $data['valor'] = $opcion;

          $entity = $configsTable->newEntity($data);


          if (!$configsTable->save($entity, ['atomic' => false])) {


            /********trying to catch database error here******/                

            throw new \Exception(__('Error message'));
          }

        }else{
          $u = $configsTable->updateAll(['valor'=>$opcion], [
            'id'=>$reg->id
          ]); 

          if(!$u){                                 

            /********trying to catch database error here******/                

            throw new \Exception(__('Error message'));
          }
        }
      }
    });

    $this->Flash->success(__('Ajustes actualizados'),[
      'params'=>['class'=>'alert-absolute timed', 'tiempo'=>5]
    ]);

  } catch (\PDOException $ex) {
    $this->Flash->error($ex->getCode().' - '.$ex->getMessage(),[
      //'params'=>['class'=>'alert-absolute timed', 'tiempo'=>5]
    ]);
  } catch (\Exception $ex){
    $this->Flash->error($ex->getMessage(),[
      //'params'=>['class'=>'alert-absolute timed', 'tiempo'=>5]
    ]);
  }
}

我仍在搜索食谱以获取一些信息。谢谢。

【问题讨论】:

    标签: php mysql cakephp


    【解决方案1】:

    PDOException 由错误处理程序处理(默认情况下完成)。您可以设置your own error handler 检查异常类型并在PDOException 的情况下发送电子邮件。

    您可以使用$error->queryString的异常实例获取sql查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多