Skip to content

Commit

Permalink
Fixes yiisoft#4198: Fixed PHP 7.2 SQLSRV lastInsertId
Browse files Browse the repository at this point in the history
  • Loading branch information
itegodev authored and samdark committed May 30, 2018
1 parent 3881ddd commit 5155678
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Version 1.1.20 under development
- Bug #4203: Fixed `CHttpCacheFilter::sendCacheControlHeader` PHP 7.2 compatibility (b1rdex)
- Enh #4191: Added option for filter classes loaded by YiiBase autoloader (daniel1302)
- Chg #4160: Updated HTMLPurifier to version 4.9.3 (takobell)
- Bug #4198: Fixed PHP 7.2 SQLSRV lastInsertId (agusdrs)

Version 1.1.19 June 8, 2017
---------------------------
Expand Down
15 changes: 10 additions & 5 deletions framework/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ class CMssqlSqlsrvPdoAdapter extends PDO
{
/**
* Returns last inserted ID value.
* SQLSRV driver supports PDO::lastInsertId() with one peculiarity: when $sequence's value is null or empty
* string it returns empty string. But when parameter is not specified at all it's working as expected
* and returns actual last inserted ID (like other PDO drivers).
* Before version 5.0, the SQLSRV driver supports PDO::lastInsertId() with one peculiarity: when $sequence's
* value is null or empty string it returns empty string. But when parameter is not specified at all it's working as
* expected and returns actual last inserted ID (like other PDO drivers).
* Version 5.0 of the Microsoft PHP Drivers for SQL Server changes the behaviour of PDO::lastInsertID to be
* consistent with the behaviour outlined in the PDO documentation. It returns the ID of the
* last inserted sequence or row.
*
* @param string|null $sequence the sequence name. Defaults to null.
* @param string|null $sequence the sequence/table name. Defaults to null.
* @return integer last inserted ID value.
*/
public function lastInsertId($sequence=null)
{
if(!$sequence)
$sqlsrvVer = phpversion('pdo_sqlsrv') ? intval(explode('.', phpversion('pdo_sqlsrv'))[0]) : 0;

if(!$sequence || $sqlsrvVer >= 5)
return parent::lastInsertId();
return parent::lastInsertId($sequence);
}
Expand Down

0 comments on commit 5155678

Please sign in to comment.