我想要的是在发生错误和其他回滚时将记录插入系统日志中。
下面是代码的主要内容:
\ Log :: error('发生错误:无法找到记录');
我有以下代码:
public function read()
{
$id = 'some-id';
try {
DB::beginTransaction();
$record = $this->getRecordsFromTableA($id);
// some mor actions are here
//
//
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
}
}
private function getRecordsFromTableA($id)
{
try {
DB::beginTransaction();
$company = TableA::find($id);
if (!$company) {
throw new ApiOperationFailedException();
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
// i don't want to rollback this change and this will add record into system_logs
\Log::error('Error occurs : unable to find record');
throw new ApiOperationFailedException($e->getMessage());
}
}
答案 0 :(得分:0)
只需从这样的读取功能中删除提交和回滚功能。
public function read()
{
DB::beginTransaction();
$id = 'some-id';
try {
$record = $this->getRecordsFromTableA($id);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
// i don't want to rollback this change and this will add record into system_logs
\Log::error('Error occurs : unable to find record');
throw new ApiOperationFailedException($e->getMessage());
}
}
答案 1 :(得分:0)
您没有插入数据库。您仅查询数据库。 DB :: rollback()在DB :: beginTransaction()之后回滚插入数据库的记录。由于没有插入任何记录,因此没有任何要回滚的记录。谢谢,祝你好运