この問題の解決策をブログで説明しました。コード内のSQLクエリを直接変更できなかったため、この方法でPDOクラスを拡張しました
class BetterPDO extends \PDO
{
protected $stmt;
protected $withIdentitySelect;
public function prepare($statement, $driver_options = null)
{
$this->$withIdentitySelect = false;
if (preg_match('/^insert/i', $statement)) {
$statement = "{$statement}; select SCOPE_IDENTITY() AS 'Identity';";
$this->$withIdentitySelect = true;
}
$this->stmt = parent::prepare($statement, is_array($driver_options) ? $driver_options : []);
return $this->stmt;
}
public function query($statement)
{
$this->$withIdentitySelect = false;
if (preg_match('/^insert/i', $statement)) {
$statement = "{$statement}; select SCOPE_IDENTITY() AS 'Identity';";
$this->$withIdentitySelect = true;
}
$this->stmt = parent::query($statement);
return $this->stmt;
}
public function lastInsertId($name = null)
{
$lastIndex = 0;
if (($this->$withIdentitySelect) && ($this->stmt->columnCount() > 0)) {
$lastIndex = $this->stmt->fetchColumn(0);
$this->stmt->closeCursor();
}
return $lastIndex;
}
}