From 1b998d54431c649f686792b9de810750e1043891 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 12 Dec 2011 00:07:28 +0100 Subject: [PATCH] Fix: Removed a bug with pgsql --- htdocs/core/class/menubase.class.php | 34 +++++++++++++------ htdocs/core/modules/DolibarrModules.class.php | 4 ++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/htdocs/core/class/menubase.class.php b/htdocs/core/class/menubase.class.php index be940844072..0d20cd1f175 100644 --- a/htdocs/core/class/menubase.class.php +++ b/htdocs/core/class/menubase.class.php @@ -58,13 +58,13 @@ class Menubase /** * Constructor * - * @param DoliDB $DB Database handler + * @param DoliDB $db Database handler * @param string $menu_handler Menu handler * @param string $type Type */ - function Menubase($DB,$menu_handler='',$type='') + function Menubase($db,$menu_handler='',$type='') { - $this->db = $DB; + $this->db = $db; $this->menu_handler = $menu_handler; $this->type = $type; return 1; @@ -101,14 +101,26 @@ class Menubase if (! $this->level) $this->level=0; // Check parameters - // Put here code to add control on parameters values + if (empty($this->menu_handler)) return -1; - // FIXME - // Get the max rowid in llx_menu and use it as rowid in insert because postgresql + // For PGSQL, we must first found the max rowid and use it as rowid in insert because postgresql // may use an already used value because its internal cursor does not increase when we do // an insert with a forced id. - // Two solution: Disable menu handler using database when database is postgresql or update counter when - // enabling such menus. + if (in_array($this->db->type,array('pgsql'))) + { + $sql = "SELECT MAX(rowid) as maxrowid FROM ".MAIN_DB_PREFIX."menu"; + $resqlrowid=$this->db->query($sql); + if ($resqlrowid) + { + $obj=$this->db->fetch_object($resqlrowid); + $maxrowid=$obj->maxrowid; + + $sql = "SELECT setval('".MAIN_DB_PREFIX."menu_rowid_seq', ".($maxrowid).")"; + $resqlrowidset=$this->db->query($sql); + if (! $resqlrowidset) dol_print_error($this->db); + } + else dol_print_error($this->db); + } // Insert request $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu("; @@ -149,19 +161,19 @@ class Menubase $sql.= " '".$this->user."'"; $sql.= ")"; - dol_syslog("Menubase::create sql=".$sql, LOG_DEBUG); + dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."menu"); - dol_syslog("Menubase::create record added has rowid=".$this->id, LOG_DEBUG); + dol_syslog(get_class($this)."::create record added has rowid=".$this->id, LOG_DEBUG); return $this->id; } else { $this->error="Error ".$this->db->lasterror(); - dol_syslog("Menubase::create ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); return -1; } } diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php index 59673f7971c..e9541291e26 100644 --- a/htdocs/core/modules/DolibarrModules.class.php +++ b/htdocs/core/modules/DolibarrModules.class.php @@ -1089,6 +1089,8 @@ abstract class DolibarrModules */ function insert_menus() { + global $user; + require_once(DOL_DOCUMENT_ROOT."/core/class/menubase.class.php"); $err=0; @@ -1149,7 +1151,7 @@ abstract class DolibarrModules if (! $err) { - $result=$menu->create(); + $result=$menu->create($user); if ($result > 0) { $this->menu[$key]['rowid']=$result;