【发布时间】:2017-09-26 12:12:49
【问题描述】:
我的表列 entry_id 列设置为唯一...
有没有最好的方法来使用 try & catch 和 DB::rollback 来修改我的变量?例如:
$_id = 1
$entryId = 'test0000'+$_id;
DB::beginTransaction();
try {
$sub = Submission::create([
'entry_id' => $entryId,
]);
DB::commit();
// success insert..
} catch (\Exception $e) {
DB::rollback();
//detected unique... just ++ _id to ensure not unique
$_id++;
// throw $e;
} catch (\Throwable $e) {
DB::rollback();
//detected unique... just ++ _id to ensure not unique
$_id++;
// throw $e;
}
第一次不会有错误,请问第二次,为了确保entry_id不唯一,我可以使用回滚方法进入++吗?这是正确的方法吗?
谢谢!
【问题讨论】: