【发布时间】: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]
]);
}
}
我仍在搜索食谱以获取一些信息。谢谢。
【问题讨论】: