Skip to content

Commit c688105

Browse files
committed
Fix lastInsertId implementation
last_insert_id returns autoincrement column values
1 parent 9afb99f commit c688105

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Processor/InsertProcessor.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ public static function process(
7878
$column = $table_definition->columns[$column_name];
7979

8080
if ($column instanceof IntegerColumn && $column->isAutoIncrement()) {
81-
$conn->getServer()->addAutoIncrementMinValue(
81+
$last_incremented_value = $conn->getServer()->addAutoIncrementMinValue(
8282
$database,
8383
$table_name,
8484
$column_name,
8585
$value
8686
);
87-
}
88-
}
8987

90-
if (\count($table_definition->primaryKeyColumns) === 1 && $conn->lastInsertId() === "0") {
91-
$conn->setLastInsertId((string) $row[$table_definition->primaryKeyColumns[0]]);
88+
if ($conn->lastInsertId() === "0") {
89+
$conn->setLastInsertId((string)$last_incremented_value);
90+
}
91+
}
9292
}
9393

9494
$table[] = $row;

src/Server.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function addAutoIncrementMinValue(
241241
string $table_name,
242242
string $column_name,
243243
int $value
244-
) : void {
244+
) : int {
245245
$table_definition = $this->getTableDefinition($database_name, $table_name);
246246
$table = $this->databases[$database_name][$table_name] ?? null;
247247

@@ -253,7 +253,7 @@ public function addAutoIncrementMinValue(
253253
$table = $this->databases[$database_name][$table_name] = new TableData();
254254
}
255255

256-
$table->autoIncrementCursors[$column_name] = max(
256+
return $table->autoIncrementCursors[$column_name] = max(
257257
$table->autoIncrementCursors[$column_name] ?? 0,
258258
$value
259259
);

0 commit comments

Comments
 (0)