database
database copied to clipboard
INSERT INTO SELECT
- bug report? yes
- feature request? no
- version: ~2.3@dev
Description
$database->query("
INSERT INTO product_stock_merchant_events (x, y, z)
SELECT
x, y, z
FROM product_stock_events
WHERE id IN (?)",
$stockEvent_ids);
when you try to call insert with select it crashes on
Invalid argument supplied for foreach()
File: ...\nette\database\src\Database\SqlPreprocessor.php:178
168: }
169:
170: if (is_array($value)) {
171: $vx = $kx = array();
172: if ($mode === 'auto') {
173: $mode = $this->arrayMode;
174: }
175:
176: if ($mode === 'values') { // (key, key, ...) VALUES (value, value, ...)
177: if (array_key_exists(0, $value)) { // multi-insert
178: foreach ($value[0] as $k => $v) {
179: $kx[] = $this->delimite($k);
180: }
181: foreach ($value as $val) {
182: $vx2 = array();
Automatic detection for INSERT try build this
(column, column) VALUES (value, value)
Let's use
$database->query("
INSERT INTO product_stock_merchant_events (x, y, z)
SELECT
x, y, z
FROM product_stock_events
WHERE id IN (?set)",
$stockEvent_ids);
this say what to do with array.
How to solve it? The source code provided above: On line 177 condition is TRUE, but on line 178 $values[0] is not array.
Throw exception with message "Automatic detection of mode faild let's define exactly set|and|or|order|values"?
Change mode from values to set|or?
Exception seems better to me.
Ok I prepare PR.
Great, thanks