From ead130910b5b8d0e639db831893be8bc7a49969f Mon Sep 17 00:00:00 2001 From: Siliang <1028281010@qq.com> Date: Wed, 24 Jul 2024 18:38:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPDO=E9=93=BE=E6=8E=A5pgsql?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=81=87=E5=88=B0=E5=B7=B2=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E8=87=AA=E5=A2=9Eid=E6=97=B6=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=88=90=E5=8A=9Fcommit=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/PDOConnection.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/db/PDOConnection.php b/src/db/PDOConnection.php index 61bf0cbd..98b8bcaf 100644 --- a/src/db/PDOConnection.php +++ b/src/db/PDOConnection.php @@ -1749,10 +1749,17 @@ public function getLastSql(): string */ public function getLastInsID(BaseQuery $query, string $sequence = null) { - try { - $insertId = $this->linkID->lastInsertId($sequence); - } catch (\Exception $e) { - $insertId = ''; + $data = $query->getOptions('data'); + $pk = $query->getAutoInc(); + if ($pk && $data[$pk]) { + // 已指定主键自增值时直接获取 + $insertId = (string) $data[$pk]; + } else { + try { + $insertId = $this->linkID->lastInsertId($sequence); + } catch (\Exception $e) { + $insertId = ''; + } } return $this->autoInsIDType($query, $insertId);