Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] In MySQL, harvest last insert ID immediately after query is executed #52390

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Illuminate/Database/MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@

class MySqlConnection extends Connection
{
/**
* The last inserted ID generated by the server
*
* @var string|int|null
*/
protected $lastInsertId;

/**
* Run an insert statement against the database.
*
* @param string $query
* @param array $bindings
* @param string|null $sequence
* @return bool
*/
public function insert($query, $bindings = [], $sequence = null)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($sequence) {
if ($this->pretending()) {
return true;
}

$statement = $this->getPdo()->prepare($query);

$this->bindValues($statement, $this->prepareBindings($bindings));

$this->recordsHaveBeenModified();

$result = $statement->execute();

$this->lastInsertId = $this->getPdo()->lastInsertId($sequence);

return $result;
});
}

public function getLastInsertId()
{
return $this->lastInsertId;
}

/**
* Escape a binary value for safe SQL embedding.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Query/Processors/MySqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Database\Query\Processors;

use Illuminate\Database\Query\Builder;

class MySqlProcessor extends Processor
{
/**
Expand All @@ -19,6 +21,24 @@ public function processColumnListing($results)
}, $results);
}

/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string|null $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$query->getConnection()->insert($sql, $values, $sequence);

$id = $query->getConnection()->getLastInsertId();

return is_numeric($id) ? (int) $id : $id;
}

/**
* Process the results of a columns query.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Database/MySql/DatabaseMySqlConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Database\MySql;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -151,4 +152,27 @@ public static function jsonContainsKeyDataProvider()
'null value' => [1, 'json_col->bar'],
];
}

public function testLastInsertIdIsPreserved()
{
if (! Schema::hasTable('auto_id_table')) {
Schema::create('auto_id_table', function (Blueprint $table) {
$table->id();
});
}

try {
static $callbackExecuted = false;
DB::listen(function (QueryExecuted $event) use (&$callbackExecuted) {
DB::getPdo()->query('SELECT 1');
$callbackExecuted = true;
});

$id = DB::table('auto_id_table')->insertGetId([]);
$this->assertTrue($callbackExecuted, 'The query listener was not executed.');
$this->assertEquals(1, $id);
} finally {
Schema::drop('auto_id_table');
}
}
}