我具有类似的功能,并且想要在失败的尝试时自动处理插入(取决于错误原因)-它必须是锁定条件。
这是用于企业的应用程序,因此我不依赖客户端服务器之间的大量通信,但仍需要可靠地处理插入。
我应该在哪里放置自动重新提交逻辑(api请求或实际的php代码中),以及如何实现它?
public function createOne($params = [])
{
$sql = "insert
into actionitems(
actionitemid,
assignorid,
ownerid,
altownerid,
duedate,
ecd,
criticality,
actionitemtitle,
actionitemstatement,
closurecriteria
)
values(
:actionitemid
:assignorid,
:ownerid,
:altownerid,
:duedate,
:ecd,
:criticality,
:actionitemtitle,
:actionitemstatement,
:closurecriteria
)";
try
{
$this->db->beginTransaction();
$this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$locksql = $this->db->query("lock tables actionitem write");
$statement = $this->db->prepare($sql);
$statement->execute([
':actionitemid' => $params['actionitemid'],
':assignorid' => $params['assignor'],
':ownerid' => $params['owner'],
':altownerid' => $params['altowner'],
':duedate' => $params['duedate'],
':ecd' => $params['ecd'],
':criticality' => $params['criticality'],
':actionitemtitle' => $params['actionitemtitle'],
':actionitemstatement' => $params['actionitemstatement'],
':closurecriteria' => $params['closurecriteria'],
]);
return json_encode(['Result' => 'Success'], JSON_PRETTY_PRINT);
$this->db->commit();
}