Fix: Removed a bug with pgsql

This commit is contained in:
Laurent Destailleur 2011-12-12 00:07:28 +01:00
parent 3f03a82e89
commit 1b998d5443
2 changed files with 26 additions and 12 deletions

View File

@ -58,13 +58,13 @@ class Menubase
/** /**
* Constructor * Constructor
* *
* @param DoliDB $DB Database handler * @param DoliDB $db Database handler
* @param string $menu_handler Menu handler * @param string $menu_handler Menu handler
* @param string $type Type * @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->menu_handler = $menu_handler;
$this->type = $type; $this->type = $type;
return 1; return 1;
@ -101,14 +101,26 @@ class Menubase
if (! $this->level) $this->level=0; if (! $this->level) $this->level=0;
// Check parameters // Check parameters
// Put here code to add control on parameters values if (empty($this->menu_handler)) return -1;
// FIXME // For PGSQL, we must first found the max rowid and use it as rowid in insert because postgresql
// Get the max rowid in llx_menu 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 // may use an already used value because its internal cursor does not increase when we do
// an insert with a forced id. // an insert with a forced id.
// Two solution: Disable menu handler using database when database is postgresql or update counter when if (in_array($this->db->type,array('pgsql')))
// enabling such menus. {
$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 // Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."menu("; $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu(";
@ -149,19 +161,19 @@ class Menubase
$sql.= " '".$this->user."'"; $sql.= " '".$this->user."'";
$sql.= ")"; $sql.= ")";
dol_syslog("Menubase::create sql=".$sql, LOG_DEBUG); dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."menu"); $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; return $this->id;
} }
else else
{ {
$this->error="Error ".$this->db->lasterror(); $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; return -1;
} }
} }

View File

@ -1089,6 +1089,8 @@ abstract class DolibarrModules
*/ */
function insert_menus() function insert_menus()
{ {
global $user;
require_once(DOL_DOCUMENT_ROOT."/core/class/menubase.class.php"); require_once(DOL_DOCUMENT_ROOT."/core/class/menubase.class.php");
$err=0; $err=0;
@ -1149,7 +1151,7 @@ abstract class DolibarrModules
if (! $err) if (! $err)
{ {
$result=$menu->create(); $result=$menu->create($user);
if ($result > 0) if ($result > 0)
{ {
$this->menu[$key]['rowid']=$result; $this->menu[$key]['rowid']=$result;