diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 1861e974576..da2493264be 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -2,6 +2,7 @@ /* Copyright (C) 2006-2012 Laurent Destailleur * Copyright (C) 2009-2012 Regis Houssin * Copyright (C) 2012 Christophe Battarel + * Copyright (C) 2012 Juanjo Menent * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -583,10 +584,19 @@ class ImportCsv extends ModeleImports //var_dump($objimport->array_import_convertvalue); exit; // Build SQL request - $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key'; - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; - $sql.=') VALUES('.$listvalues.", '".$importid."'"; - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id; + if (! tablewithentity($tablename)) + { + $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key'; + if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; + $sql.=') VALUES('.$listvalues.", '".$importid."'"; + } + else + { + $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key, entity'; + if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; + $sql.=') VALUES('.$listvalues.", '".$importid."', ".$conf->entity ; + } + if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id; $sql.=')'; dol_syslog("import_csv.modules sql=".$sql); @@ -637,4 +647,34 @@ function cleansep($value) return str_replace(',','/',$value); }; +/** + * Returns if a table contains entity column + * + * @param string $table Table name + * @return int 1 if table contains entity, 0 if not and -1 if error + */ +function tablewithentity($table) +{ + global $db; + $sql = "SHOW COLUMNS FROM ".$table." LIKE 'entity'"; + + $resql=$db->query($sql); + if ($resql) + { + $numrows=$db->num_rows($resql); + if ($numrows) + { + return 1; + } + else + { + return 0; + } + } + else + { + return -1; + } +} + ?>