使用Doctrine \ DBAL \ Exception捕获数据库错误 [英] Catch db error with Doctrine\DBAL\Exception
问题描述
我想从 insert
语句或其他任何错误中捕获任何错误(即:外键错误).如何使用 use Doctrine \ DBAL \ Exception
实现?
I want to catch any error (ie: foreign key error) from an insert
statement or any other. How can I achive that with use Doctrine\DBAL\Exception
?
当我执行 insert
时,我有这个:
I have this when I do the insert
:
$db->beginTransaction();
try {
$db->insert('bbc5.produccionpry',$datos);
$datos['propryid'] = $db->lastInsertId();
$db->commit();
// $db = null;
$resp[] = $datos;
} catch (Exception $e) {
$error = array_merge($error, array('error' => $e->errorInfo()));
$db->rollback();
throw $e;
}
但是,这并不能阻止具体5返回一个告诉错误的网站,所以,我不想显示该网站,我想在 array()
以便通过 echo json_encode($ error)
But, that doesn't prevent the concrete5 to return
a website telling about the error, so, I don't want that website to be shown, I want to catch the error in an array()
in order to return it via echo json_encode($error)
我没有将控制器用于页面,而是将其用于通过以下代码管理来自我的JavaScript应用的RESTful调用:
I'm not using the controller for a page, I'm using it for managing RESTful calls from my JavaScript App with this code:
return fetch(`/scamp/index.php/batchprodpry/${maq}`, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(this.state.a)
})
我正在使用ReactJS
I'm using ReactJS
谢谢
推荐答案
不要抛出异常.
不是抛出异常,而是从 DBALException $ e
对象获取异常消息 $ e-> getMessage()
,并将其编码为JSON字符串.重要提示::在 echo
之后放置一个 exit;
,以确保不再执行任何代码.
Instead of throwing the exception, just get the exceptions message $e->getMessage()
from the DBALException $e
object and encode it as a JSON string. Important: Put an exit;
after the echo
to assure that no further code is executed.
use Doctrine\DBAL\DBALException;
try {
$db->insert('bbc5.produccionpry',$datos);
$datos['propryid'] = $db->lastInsertId();
$db->commit();
$resp[] = $datos;
}
catch(DBALException $e){
$db->rollback();
echo \Core::make('helper/json')->encode($e->getMessage());
exit;
}
如果此代码位于页面控制器内部,则可以执行以下操作:
If this code is inside a page controller you could do this:
try {
$db->insert('bbc5.produccionpry',$datos);
$datos['propryid'] = $db->lastInsertId();
$db->commit();
$resp[] = $datos;
}
catch(DBALException $e){
$this->error->add($e->getMessage());
}
if($this->error->has()) {
// All variables that have been set in the view() method will be set again.
// That is why we call the view method again
$this->view();
return;
}
concrete5将负责显示适当的错误消息.
And concrete5 will take care of displaying an appropriate error message.
或者您可以在会话中保存 $ e-> getMesssage()
并在视图内调用它:
Or you could save the $e->getMesssage()
in the session and call it inside the view:
$session = \Core::make('session');
// ...
catch(Exception $e){
$session->set('error', $e->getMessage());
}
在视图中:
// html
<?php
$session = \Core::make('session');
if($session->has('error')) {
$m = $session->get('error');
?>
<div id="session_error" class="alert alert-danger">
<a href="#" class="close">×</a>
<div id="session_error_msg">
<?php print $m ?>
</div>
</div>
<?php
}
$session->remove('error');
?>
//html
这篇关于使用Doctrine \ DBAL \ Exception捕获数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!