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

[5.2] Work around lack of lastInsertId() for ODBC with PDO for MSSQL #13423

Merged
merged 4 commits into from
May 6, 2016
Merged
Changes from 3 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
15 changes: 12 additions & 3 deletions src/Illuminate/Database/Query/Processors/SqlServerProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Query\Processors;

use Illuminate\Database\Query\Builder;
use Exception;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please order these by length

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shortest first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.


class SqlServerProcessor extends Processor
{
Expand All @@ -17,9 +18,17 @@ class SqlServerProcessor extends Processor
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$query->getConnection()->insert($sql, $values);

$id = $query->getConnection()->getPdo()->lastInsertId();
$connection = $query->getConnection();
$connection->insert($sql, $values);
if ($connection->getConfig('odbc') === true) {
$result = $connection->select('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid');
if (! $result) {
throw new Exception('Error retrieving lastInsertId');
}
$id = $result[0]->insertid;
} else {
$id = $connection->getPdo()->lastInsertId();
}

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