From fe62e038cadd656ccfefe2f04dd0632fd935a1f0 Mon Sep 17 00:00:00 2001
From: Stephen L <lrq3000@gmail.com>
Date: Sun, 22 Feb 2015 14:34:51 +0100
Subject: [PATCH] Fix lastrowid-*table* option when defining imports (fix
 extrafields fk_object value)

Signed-off-by: Stephen L. <lrq3000@gmail.com>
---
 htdocs/core/modules/import/import_csv.modules.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php
index 14de889d9ce..d487d3f8aef 100644
--- a/htdocs/core/modules/import/import_csv.modules.php
+++ b/htdocs/core/modules/import/import_csv.modules.php
@@ -354,6 +354,7 @@ class ImportCsv extends ModeleImports
 		}
 		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)
 			// For each table to insert, me make a separate insert
 			foreach($objimport->array_import_tables[0] as $alias => $tablename)
 			{
@@ -581,7 +582,7 @@ class ImportCsv extends ModeleImports
     				    elseif (preg_match('/^lastrowid-/',$val))
     				    {
     				        $tmp=explode('-',$val);
-                            $lastinsertid=$this->db->last_insert_id($tmp[1]);
+    				        $lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0;
     				        $listfields.=preg_replace('/^'.preg_quote($alias).'\./','',$key);
                             $listvalues.=$lastinsertid;
     				        //print $key."-".$val."-".$listfields."-".$listvalues."<br>";exit;
@@ -623,6 +624,7 @@ class ImportCsv extends ModeleImports
 						if ($sql)
 						{
 							$resql=$this->db->query($sql);
+							$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)
 							{
 								//print '.';
-- 
GitLab