[*] All : Now autoExecute allow inserting multiple row

This commit is contained in:
mDeflotte committed 2011-10-07 12:55:34 +00:00
1 parent 786ee5e553
commit 59dfcb3609
1 file changed
+24 -5
+24 -5
View File
@@ -217,7 +217,7 @@ abstract class DbCore
* Filter SQL query within a blacklist * Filter SQL query within a blacklist
* *
* @param string $table Table where insert/update data * @param string $table Table where insert/update data
* @param string $values Data to insert/update * @param string $data Data to insert/update
* @param string $type INSERT or UPDATE * @param string $type INSERT or UPDATE
* @param string $where WHERE clause, only for UPDATE (optional) * @param string $where WHERE clause, only for UPDATE (optional)
* @param string $limit LIMIT clause (optional) * @param string $limit LIMIT clause (optional)
@@ -231,14 +231,33 @@ abstract class DbCore
if (strtoupper($type) == 'INSERT') if (strtoupper($type) == 'INSERT')
{ {
$keys = $values = array(); // Check if $data is a list of row
foreach ($data AS $key => $value) if (!is_array(current($data)))
$data = array($data);
$keys = array();
$values_stringified = array();
foreach ($data as $row_data)
{ {
$values = array();
foreach ($row_data AS $key => $value)
{
if (isset($keys_stringified))
{
// Check if row array mapping are the same
if (!in_array("`$key`", $keys))
throw new PrestashopDatabaseException('Keys form $data subarray don\'t match');
}
else
$keys[] = "`$key`"; $keys[] = "`$key`";
$values[] = ($useNull && ($value === '' || is_null($value))) ? 'NULL' : "'$value'"; $values[] = ($useNull && ($value === '' || is_null($value))) ? 'NULL' : "'$value'";
} }
$keys_stringified = implode(', ', $keys);
$values_stringified[] = '('.implode(', ', $values).')';
}
$sql = 'INSERT INTO `'.$table.'` ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; $sql = 'INSERT INTO `'.$table.'` ('.$keys_stringified.') VALUES '.implode(', ', $values_stringified);
if ($limit) if ($limit)
$sql .= ' LIMIT '.(int)$limit; $sql .= ' LIMIT '.(int)$limit;
return $this->q($sql, $use_cache); return $this->q($sql, $use_cache);
@@ -256,7 +275,7 @@ abstract class DbCore
return $this->q($sql, $use_cache); return $this->q($sql, $use_cache);
} }
else else
die('Wrong argument (miss type) in Db::autoExecute()'); throw new PrestashopDatabaseException('Wrong argument (miss type) in Db::autoExecute()');
return false; return false;
} }