summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaspars Foigts <[email protected]>2012-12-19 16:16:47 +0200
committerSimon Holywell <[email protected]>2013-01-03 13:08:28 +0000
commitd610dc89969bb3c006f38da8c96bf6484eca6104 (patch)
treedd1c50a5337d607761c100cf9298a616839e7bad
parentbc3856eebbfeaca821eeddfd1abf384278553851 (diff)
Fix last insert ID not being returned with postgresql, take 2.
-rw-r--r--README.markdown1
-rw-r--r--idiorm.php11
2 files changed, 11 insertions, 1 deletions
diff --git a/README.markdown b/README.markdown
index 34fb6fe..cc1414b 100644
--- a/README.markdown
+++ b/README.markdown
@@ -43,6 +43,7 @@ Changelog
* Add in raw_execute - closes issue #40 [[tag](https://github.com/tag)]
* Add query logging to `delete_many` [[tag](https://github.com/tag)]
* Add `is_new` method - closes issue #85
+* Fix last insert ID for PostgreSQL using RETURNING - closes issues #62 and #89 [[laacz](https://github.com/laacz)]
#### 1.2.3 - release 2012-11-28
diff --git a/idiorm.php b/idiorm.php
index 23e297c..6492b7f 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -1323,7 +1323,11 @@
if ($this->_is_new) {
$this->_is_new = false;
if (is_null($this->id())) {
- $this->_data[$this->_get_id_column_name()] = self::$_db->lastInsertId();
+ if (self::$_db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {
+ $this->_data[$this->_get_id_column_name()] = $statement->fetchColumn();
+ } else {
+ $this->_data[$this->_get_id_column_name()] = self::$_db->lastInsertId();
+ }
}
}
@@ -1364,6 +1368,11 @@
$placeholders = $this->_create_placeholders($this->_dirty_fields);
$query[] = "({$placeholders})";
+
+ if (self::$_db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {
+ $query[] = 'RETURNING ' . $this->_quote_identifier($this->_get_id_column_name());
+ }
+
return join(" ", $query);
}