Fix Import INSERT foreign key

Check the row referencing this foreign key exists first
instead of always trying to perform an UPDATE.
This commit is contained in:
François J 2018-03-26 17:52:43 +02:00
parent 1c581c6fff
commit e892089afd
2 changed files with 54 additions and 3 deletions

View File

@ -590,6 +590,7 @@ class ImportCsv extends ModeleImports
$insertdone = false; $insertdone = false;
if (!empty($updatekeys)) { 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) // 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)) { if (empty($lastinsertid)) {
$sqlSelect = 'SELECT rowid FROM '.$tablename; $sqlSelect = 'SELECT rowid FROM '.$tablename;
@ -625,6 +626,31 @@ class ImportCsv extends ModeleImports
$this->errors[$error]['type']='SQL'; $this->errors[$error]['type']='SQL';
$error++; $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)) { if (!empty($lastinsertid)) {

View File

@ -649,6 +649,31 @@ class ImportXlsx extends ModeleImports
$this->errors[$error]['type']='SQL'; $this->errors[$error]['type']='SQL';
$error++; $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)) { if (!empty($lastinsertid)) {