From e892089afd5721e3816aee7acc37bb8ea4135aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20J?= Date: Mon, 26 Mar 2018 17:52:43 +0200 Subject: [PATCH 1/3] Fix Import INSERT foreign key Check the row referencing this foreign key exists first instead of always trying to perform an UPDATE. --- .../modules/import/import_csv.modules.php | 32 +++++++++++++++++-- .../modules/import/import_xlsx.modules.php | 25 +++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 96f0dca04fa..0d8f700d7e0 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -590,6 +590,7 @@ class ImportCsv extends ModeleImports $insertdone = false; if (!empty($updatekeys)) { // We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields) + if (empty($lastinsertid)) { $sqlSelect = 'SELECT rowid FROM '.$tablename; @@ -603,7 +604,7 @@ class ImportCsv extends ModeleImports $filters[] = $col.' = '.$data[$key]; } $sqlSelect.= ' WHERE '.implode(' AND ', $where); - + $resql=$this->db->query($sqlSelect); if($resql) { $res = $this->db->fetch_object($resql); @@ -625,8 +626,33 @@ class ImportCsv extends ModeleImports $this->errors[$error]['type']='SQL'; $error++; } + } else { + // We have a last INSERT ID. Check if we have a row referencing this foreign key. + $sqlSelect = 'SELECT rowid FROM '.$tablename; + + if(empty($keyfield)) $keyfield = 'rowid'; + $sqlSelect .= ' WHERE '.$keyfield.' = '.$lastinsertid; + + $resql=$this->db->query($sqlSelect); + if($resql) { + $res = $this->db->fetch_object($resql); + if($resql->num_rows == 1) { + // We have a row referencing this last foreign key, continue with UPDATE. + } else { + // No record found referencing this last foreign key, + // force $lastinsertid to 0 so we INSERT below. + $lastinsertid = 0; + } + } + else + { + //print 'E'; + $this->errors[$error]['lib']=$this->db->lasterror(); + $this->errors[$error]['type']='SQL'; + $error++; + } } - + if (!empty($lastinsertid)) { // Build SQL UPDATE request $sqlstart = 'UPDATE '.$tablename; @@ -640,7 +666,7 @@ class ImportCsv extends ModeleImports if(empty($keyfield)) $keyfield = 'rowid'; $sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid; - + $sql = $sqlstart.$sqlend; // Run update request diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 22b81fcdcbf..18cf7c767ac 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -649,6 +649,31 @@ class ImportXlsx extends ModeleImports $this->errors[$error]['type']='SQL'; $error++; } + } else { + // We have a last INSERT ID. Check if we have a row referencing this foreign key. + $sqlSelect = 'SELECT rowid FROM '.$tablename; + + if(empty($keyfield)) $keyfield = 'rowid'; + $sqlSelect .= ' WHERE '.$keyfield.' = '.$lastinsertid; + + $resql=$this->db->query($sqlSelect); + if($resql) { + $res = $this->db->fetch_object($resql); + if($resql->num_rows == 1) { + // We have a row referencing this last foreign key, continue with UPDATE. + } else { + // No record found referencing this last foreign key, + // force $lastinsertid to 0 so we INSERT below. + $lastinsertid = 0; + } + } + else + { + //print 'E'; + $this->errors[$error]['lib']=$this->db->lasterror(); + $this->errors[$error]['type']='SQL'; + $error++; + } } if (!empty($lastinsertid)) { From 86c4bb23adeb6dffe446314e7a623e9cd83eb59e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 27 Mar 2018 17:46:28 +0200 Subject: [PATCH 2/3] Update import_csv.modules.php --- htdocs/core/modules/import/import_csv.modules.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 0d8f700d7e0..2005b5bb56c 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -628,6 +628,9 @@ class ImportCsv extends ModeleImports } } else { // We have a last INSERT ID. Check if we have a row referencing this foreign key. + // This is required when updating table with some extrafields. When inserting a record in parent table, we can make + // a direct insert into subtable extrafields, but when me wake an update, the insertid is defined and the child record + // may already exists. So we rescan the extrafield table to be know if record exists or not for the rowid. $sqlSelect = 'SELECT rowid FROM '.$tablename; if(empty($keyfield)) $keyfield = 'rowid'; From d41908e52a5263a893572ab577a45baa86ada2ed Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 27 Mar 2018 17:46:50 +0200 Subject: [PATCH 3/3] Update import_xlsx.modules.php --- htdocs/core/modules/import/import_xlsx.modules.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 18cf7c767ac..80436abacd5 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -651,6 +651,9 @@ class ImportXlsx extends ModeleImports } } else { // We have a last INSERT ID. Check if we have a row referencing this foreign key. + // This is required when updating table with some extrafields. When inserting a record in parent table, we can make + // a direct insert into subtable extrafields, but when me wake an update, the insertid is defined and the child record + // may already exists. So we rescan the extrafield table to be know if record exists or not for the rowid. $sqlSelect = 'SELECT rowid FROM '.$tablename; if(empty($keyfield)) $keyfield = 'rowid';