Merge pull request #8453 from OPEN-DSI/fix-import-insert-foreign-key

FIX #7581 Import INSERT foreign key
This commit is contained in:
Laurent Destailleur 2018-03-27 17:47:45 +02:00 committed by GitHub
commit 6eca679f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -591,6 +591,7 @@ class ImportCsv extends ModeleImports
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;
@ -626,6 +627,34 @@ 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.
// 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';
$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)) {

View File

@ -651,6 +651,34 @@ 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.
// 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';
$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)) {