Skip to content

Commit

Permalink
Fixed issue #13821: When importing a survey I receive a "Failed to In…
Browse files Browse the repository at this point in the history
…sert [3]" error message (#1090)

Dev: This issue is caused by a change in the sql pdo driver
Dev: without this fix, new ids aren't returned from tables with identity columns in mssql
Dev: This causes questions not to save correctly (import, new etc)
Dev: This fix is from the yii 1.19-dev label, commit:
Dev: Commit 7b768c304289ede574768de68939224d1180e2b1
  • Loading branch information
githubLewis authored and LouisGac committed Jul 9, 2018
1 parent 0501b31 commit d69915c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions framework/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php
Expand Up @@ -20,16 +20,22 @@ 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)
$parts = explode('.', phpversion('pdo_sqlsrv'));
$sqlsrvVer = phpversion('pdo_sqlsrv') ? intval(array_shift($parts)) : 0;

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

0 comments on commit d69915c

Please sign in to comment.