method insertUpdate documentation in bbn\Db\Languages\Sql

If not exist inserts row(s) in a table, else update.

$db->insertUpdate( "table_users", [ 'id' => '12', 'name' => 'Frank' ] );

function($table, array $values = NULL) { if (empty($table)) { throw new \Exception(X::_('Table name is not specified')); } // Twice the arguments if (\is_array($table) && isset($table['values'])) { $values = $table['values']; } if (!X::isAssoc($values)) { $res = 0; foreach ($values as $v){ $res += $this->insertUpdate( is_array($table) ? ($table['tables'][0] ?? $table['table'][0] ?? '') : $table, $v ); } return $res; } $keys = $this->getKeys($table); $unique = []; foreach ($keys['keys'] as $k){ // Checking each unique key if ($k['unique']) { $i = 0; foreach ($k['columns'] as $c){ if (isset($values[$c])) { $unique[$c] = $values[$c]; $i++; } } // Only if the number of known field values matches the number of columns // which are parts of the unique key // If a value is null it won't pass isset and so won't be used if (($i === \count($k['columns'])) && $this->count($table, $unique)) { // Removing unique matching fields from the values (as it is the where) foreach ($unique as $f => $v){ unset($values[$f]); } // For updating return $this->update($table, $values, $unique); } } } // No need to update, inserting return $this->insert($table, $values); }

If not exist inserts row(s) in a table, else update. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.