nette / database

💾 A database layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.
https://doc.nette.org/database
Other
513 stars 108 forks source link

INSERT INTO SELECT #171

Open matak opened 7 years ago

matak commented 7 years ago

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();
h4kuna commented 7 years ago

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.

h4kuna commented 7 years ago

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?

dg commented 7 years ago

Exception seems better to me.

h4kuna commented 7 years ago

Ok I prepare PR.

dg commented 7 years ago

Great, thanks