From dcf1a8f8cf50b89b57f69b1f176b5d32519f6808 Mon Sep 17 00:00:00 2001 From: Kaspars Foigts Date: Wed, 19 Dec 2012 16:16:47 +0200 Subject: [PATCH] Fix last insert ID not being returned with postgresql, take 2. --- idiorm.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/idiorm.php b/idiorm.php index 9d1f8c45..2893e4eb 100644 --- a/idiorm.php +++ b/idiorm.php @@ -1288,7 +1288,11 @@ public function save() { 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(); + } } } @@ -1329,6 +1333,11 @@ protected function _build_insert() { $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); }