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语句或任何其他语句中捕获任何错误(即:外键错误)。如何使用Doctrine \ DBAL \ Exception来实现?
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;
}
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)
但是,这并不妨碍concrete5返回一个告诉错误的网站,所以,我不希望显示该网站,我想在数组()中捕获错误,以便通过echo json_encode返回它($错误)
I'm not using the controller for a page, I'm using it for managing RESTful calls from my JavaScript App with this code:
我没有将控制器用于页面,我使用它来管理来自我的JavaScript应用程序的RESTful调用,使用以下代码:
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)
})
I'm using ReactJS
我正在使用ReactJS
Thank you
谢谢
2 个解决方案
#1
2
Don't throw the exception.
不要抛出异常。
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.
不要抛出异常,只需从DBALException $ e对象中获取异常消息$ e-> getMessage()并将其编码为JSON字符串。重要提示:退出;在echo之后确保没有执行进一步的代码。
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;
}
And concrete5 will take care of displaying an appropriate error message.
而concrete5将负责显示相应的错误消息。
Or you could save the $e->getMesssage()
in the session and call it inside the view:
或者你可以在会话中保存$ e-> getMesssage()并在视图中调用它:
$session = \Core::make('session');
// ...
catch(Exception $e){
$session->set('error', $e->getMessage());
}
And in the view:
在视图中:
// 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
#2
3
Since you have throw $e
in your catch
block you throw this exception further which means that it is (probably) handled by global exception listener. Once you throw exception you exit your code immediately, so a solution for you is just remove throw $e
line and you should be good
由于你在catch块中抛出了$ e,因此你会进一步抛出这个异常,这意味着它(可能)由全局异常监听器处理。一旦你抛出异常就立即退出你的代码,所以一个解决方案就是删除throw $ e行,你应该很好
#1
2
Don't throw the exception.
不要抛出异常。
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.
不要抛出异常,只需从DBALException $ e对象中获取异常消息$ e-> getMessage()并将其编码为JSON字符串。重要提示:退出;在echo之后确保没有执行进一步的代码。
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;
}
And concrete5 will take care of displaying an appropriate error message.
而concrete5将负责显示相应的错误消息。
Or you could save the $e->getMesssage()
in the session and call it inside the view:
或者你可以在会话中保存$ e-> getMesssage()并在视图中调用它:
$session = \Core::make('session');
// ...
catch(Exception $e){
$session->set('error', $e->getMessage());
}
And in the view:
在视图中:
// 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
#2
3
Since you have throw $e
in your catch
block you throw this exception further which means that it is (probably) handled by global exception listener. Once you throw exception you exit your code immediately, so a solution for you is just remove throw $e
line and you should be good
由于你在catch块中抛出了$ e,因此你会进一步抛出这个异常,这意味着它(可能)由全局异常监听器处理。一旦你抛出异常就立即退出你的代码,所以一个解决方案就是删除throw $ e行,你应该很好