Import update, new way of working with a select
This commit is contained in:
parent
d18adc1231
commit
5f704fe5e5
@ -303,6 +303,8 @@ class ImportCsv extends ModeleImports
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
$last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id)
|
$last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id)
|
||||||
|
$updatedone = false;
|
||||||
|
$insertdone = false;
|
||||||
// For each table to insert, me make a separate insert
|
// For each table to insert, me make a separate insert
|
||||||
foreach($objimport->array_import_tables[0] as $alias => $tablename)
|
foreach($objimport->array_import_tables[0] as $alias => $tablename)
|
||||||
{
|
{
|
||||||
@ -548,7 +550,8 @@ class ImportCsv extends ModeleImports
|
|||||||
{
|
{
|
||||||
$tmp=explode('-',$val);
|
$tmp=explode('-',$val);
|
||||||
$lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0;
|
$lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0;
|
||||||
$listfields[] = preg_replace('/^'.preg_quote($alias).'\./','',$key);
|
$keyfield = preg_replace('/^'.preg_quote($alias).'\./','',$key);
|
||||||
|
$listfields[] = $keyfield;
|
||||||
$listvalues[] = $lastinsertid;
|
$listvalues[] = $lastinsertid;
|
||||||
//print $key."-".$val."-".$listfields."-".$listvalues."<br>";exit;
|
//print $key."-".$val."-".$listfields."-".$listvalues."<br>";exit;
|
||||||
}
|
}
|
||||||
@ -563,32 +566,33 @@ class ImportCsv extends ModeleImports
|
|||||||
if (!empty($listfields))
|
if (!empty($listfields))
|
||||||
{
|
{
|
||||||
$updatedone = false;
|
$updatedone = false;
|
||||||
|
$insertdone = false;
|
||||||
if(!empty($updatekeys)) {
|
if(!empty($updatekeys)) {
|
||||||
// Build SQL UPDATE request
|
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
|
||||||
$sqlstart = 'UPDATE '.$tablename;
|
if(empty($lastinsertid)) {
|
||||||
|
$sqlSelect = 'SELECT rowid FROM '.$tablename;
|
||||||
|
|
||||||
$data = array_combine($listfields, $listvalues);
|
$data = array_combine($listfields, $listvalues);
|
||||||
$set = array();
|
|
||||||
foreach ($data as $key => $val) {
|
|
||||||
$set[] = $key.' = '.$val;
|
|
||||||
}
|
|
||||||
$sqlstart.= ' SET '.implode(', ', $set);
|
|
||||||
|
|
||||||
$where = array();
|
$where = array();
|
||||||
|
$filters = array();
|
||||||
foreach ($updatekeys as $key) {
|
foreach ($updatekeys as $key) {
|
||||||
|
$col = $objimport->array_import_updatekeys[0][$key];
|
||||||
$key=preg_replace('/^.*\./i','',$key);
|
$key=preg_replace('/^.*\./i','',$key);
|
||||||
$where[] = $key.' = '.$data[$key];
|
$where[] = $key.' = '.$data[$key];
|
||||||
|
$filters[] = $col.' = '.$data[$key];
|
||||||
}
|
}
|
||||||
$sqlend = ' WHERE '.implode(' AND ', $where);
|
$sqlSelect.= ' WHERE '.implode(' AND ', $where);
|
||||||
|
|
||||||
$sql = $sqlstart.$sqlend;
|
$resql=$this->db->query($sqlSelect);
|
||||||
|
|
||||||
// Run update request
|
|
||||||
$resql=$this->db->query($sql);
|
|
||||||
if($resql) {
|
if($resql) {
|
||||||
if($this->db->db->affected_rows > 0) {
|
$res = $this->db->fetch_object($resql);
|
||||||
$this->nbupdate++;
|
if($resql->num_rows == 1) {
|
||||||
$updatedone = true;
|
$lastinsertid = $res->rowid;
|
||||||
|
$last_insert_id_array[$tablename] = $lastinsertid;
|
||||||
|
} else {
|
||||||
|
$this->errors[$error]['lib']=$langs->trans('MultipleRecordFoundWithTheseFilters', implode($filter, ', '));
|
||||||
|
$this->errors[$error]['type']='SQL';
|
||||||
|
$error++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -600,8 +604,41 @@ class ImportCsv extends ModeleImports
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!empty($lastinsertid)) {
|
||||||
|
// Build SQL UPDATE request
|
||||||
|
$sqlstart = 'UPDATE '.$tablename;
|
||||||
|
|
||||||
|
$data = array_combine($listfields, $listvalues);
|
||||||
|
$set = array();
|
||||||
|
foreach ($data as $key => $val) {
|
||||||
|
$set[] = $key.' = '.$val;
|
||||||
|
}
|
||||||
|
$sqlstart.= ' SET '.implode(', ', $set);
|
||||||
|
|
||||||
|
if(empty($keyfield)) $keyfield = 'rowid';
|
||||||
|
$sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
|
||||||
|
|
||||||
|
$sql = $sqlstart.$sqlend;
|
||||||
|
|
||||||
|
// Run update request
|
||||||
|
$resql=$this->db->query($sql);
|
||||||
|
if($resql) {
|
||||||
|
if($this->db->db->affected_rows > 0) {
|
||||||
|
$updatedone = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//print 'E';
|
||||||
|
$this->errors[$error]['lib']=$this->db->lasterror();
|
||||||
|
$this->errors[$error]['type']='SQL';
|
||||||
|
$error++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update not done, we do insert
|
// Update not done, we do insert
|
||||||
if(!$updatedone) {
|
if(!$error && !$updatedone) {
|
||||||
// Build SQL INSERT request
|
// Build SQL INSERT request
|
||||||
$sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
|
$sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
|
||||||
$sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
|
$sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
|
||||||
@ -623,7 +660,7 @@ class ImportCsv extends ModeleImports
|
|||||||
$last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
|
$last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
|
||||||
if ($resql)
|
if ($resql)
|
||||||
{
|
{
|
||||||
$this->nbinsert++;
|
$insertdone = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -643,6 +680,9 @@ class ImportCsv extends ModeleImports
|
|||||||
|
|
||||||
if ($error) break;
|
if ($error) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($updatedone) $this->nbupdate++;
|
||||||
|
if($insertdone) $this->nbinsert++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@ -124,3 +124,4 @@ FormatControlRule=Format control rule
|
|||||||
KeysToUseForUpdates=Key to use for updating data
|
KeysToUseForUpdates=Key to use for updating data
|
||||||
NbInsert=Number of inserted lines: %s
|
NbInsert=Number of inserted lines: %s
|
||||||
NbUpdate=Number of updated lines: %s
|
NbUpdate=Number of updated lines: %s
|
||||||
|
MultipleRecordFoundWithTheseFilters=Multiple records have been found with these filters: %s
|
||||||
Loading…
Reference in New Issue
Block a user