Skip to content

Commit

Permalink
Fix getting last insert id for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
eSlider committed Nov 8, 2016
1 parent d5fde89 commit 324c3e2
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions Entity/FeatureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,32 +219,18 @@ public function insert($featureData)
if ($lastId < 1) {
switch ($connection->getDatabasePlatform()->getName()) {
case self::POSTGRESQL_PLATFORM:
$sql = "SELECT currval(pg_get_serial_sequence('" . $tableName . "','" . $this->getUniqueId() . "'))";
$fullTableName = $this->driver->getTableName();
$fullUniqueIdName = $connection->quoteIdentifier($this->getUniqueId());
$sql = /** @lang PostgreSQL */
"SELECT $fullUniqueIdName
FROM $fullTableName
LIMIT 1
OFFSET (SELECT count($fullUniqueIdName)-1 FROM $fullTableName )";
$lastId = $connection->fetchColumn($sql);
if ($lastId < 1) {
switch ($connection->getDatabasePlatform()->getName()) {
case self::POSTGRESQL_PLATFORM:
$sql = "SELECT currval(pg_get_serial_sequence('" . $tableName . "','" . $this->getUniqueId() . "'))";
$lastId = $connection->executeQuery($sql)->fetchColumn();
if ($lastId < 1) {
$fullTableName = '"' . $tableName . '"';
$fullUniqueIdName = $fullTableName . '."' . $this->getUniqueId() . '"';
$sql = /** @lang SQL */ "
SELECT $fullUniqueIdName
FROM $fullTableName
LIMIT 1
OFFSET (
SELECT count($fullUniqueIdName)-1
FROM $fullTableName
)";
$lastId = $connection->executeQuery($sql)->fetchColumn();
}
break;
}
}
break;
}
}

$feature->setId($lastId);
if (isset($this->events['onAfterInsert'])) {
$this->secureEval($this->events['onAfterInsert'], $event);
Expand Down

0 comments on commit 324c3e2

Please sign in to comment.