ModifierTrait::insertOrUpdate

@param array $sets

@return bool
195970
public function insertOrUpdate(array $sets)
    {
        if ($result = $this->qb->from($this->table)->getWhere($sets)) {
            return $this->update($sets);
        } else {
            return $this->insert($sets);
        }

        return false;
    }
1031718
public function modifyValues($modifiers, $subKey = null)
    {
        $process = function (&$source) use ($modifiers) {
            foreach ($modifiers as $key => $value) {
                if (is_callable($value)) {
                    $source[$key] = $value((isset($source[$key]) ? $source[$key] : null));
                } else {
                    $source[$key] = $value;
                }
            }
        };

        if (is_array($modifiers)) {

            if (is_null($subKey) || !isset($this->data[$subKey])) {
                $process($this->data);
            } else {

                $wrongSubKey = new \InvalidArgumentException(
                    sprintf("Wrong subKey given (%s), have no array value by this sub key", $subKey));;

                if (!is_array($this->data[$subKey])) {
                    throw $wrongSubKey;
                }

                array_map(function ($el) use ($wrongSubKey) {
                    if(!is_array($el)) throw $wrongSubKey;
                }, $this->data[$subKey]);

                foreach ($this->data[$subKey] as $key => $data) {
                    $process($this->data[$subKey][$key]);
                }
            }
        }

        return $this;
    }
11909
private static function updateSet(array $values)
    {
        $ret = "";
        foreach($values as $key=>$value)
            $ret .= $key."=".$value.", ";
        $ret = substr($ret,0,-2);//remove last \", \"
        return $ret;
    }
828993
public function update($fields) {
        if (is_array($fields)) {
            foreach ($fields as $key => $value) {
                if (is_string($value)) {
                    array_push($this->_updates, $key . ' = \'' . addslashes($value) . '\'');
                } else {
                    array_push($this->_updates, $key . ' = ' . $value);
                }
            }
        } else {
            return false;
        }
        
        $sql_query = 'UPDATE ' . $this->_table . 
                     ' SET ' . implode($this->_updates, ', ');
        
        // Where statements
        if (count($this->_where)) {
            $sql_query .= ' WHERE ';
            $sql_query .= implode($this->_where, ' AND ');
            
            // or statements
            if (count($this->_or)) {
                $sql_query .= ' OR ';
                $sql_query .= implode($this->_or, ' AND ');
            }
        }
        
        // Set the query
        $this->_sql_query = $sql_query;
        
        return $this;
    }
1203575
function setadd(set, element) {
  if (set.has(element)) return false;
  set.add(element);
  return true;
}
582156
public function update(array $set, $where = true)
    {
        if (empty($set)) {
            return 0;
        }
        if ($this->map) {
            $set = $this->map->set($set);
            $where = $this->map->where($where);
        }
        if ($this->fake) {
            return $this->fake->update($set, $where);
        }
        $pattern = 'UPDATE ?t SET ?sn WHERE ?w';
        $data = array($this->name, $set, $where);
        return $this->db->query($pattern, $data)->ar();
    }
900105
public function insert(array $set)
    {
        if (empty($set)) {
            return null;
        }
        if ($this->accum !== null) {
            $this->accum[] = $set;
            if (count($this->accum) >= $this->sizeAccum) {
                $this->multiInsert($this->accum, true);
                $this->accum = array();
            }
            $this->lastIdAccum++;
            return $this->lastIdAccum;
        }
        if ($this->map) {
            $set = $this->map->set($set);
        }
        if ($this->fake) {
            return $this->fake->insert($set);
        }
        $pattern = 'INSERT INTO ?t (?cols) VALUES (?ln)';
        $data = array($this->name, array_keys($set), array_values($set));
        return $this->db->query($pattern, $data)->id();
    }
824051
public function update(array $sets, $conditions = [])
    {
        if (count($sets)) {
            $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';

            if (empty($conditions)) {
                if (isset($sets[ $primaryKey ])) {
                    $conditions = [$primaryKey => $sets[ $primaryKey ]];
                }
            }

            if (method_exists($this, 'updateRecordSets')) {
                $this->updateRecordSets($sets);
            }

            if (method_exists($this, 'beforeUpdate')) {
                $sets = $this->beforeUpdate($sets);
            }

            if (method_exists($this, 'getRecordOrdering')) {
                if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) {
                    $sets[ 'record_ordering' ] = $this->getRecordOrdering($this->table);
                }
            }

            if ($this->qb->table($this->table)->update($sets, $conditions)) {

                if (method_exists($this, 'afterUpdate')) {
                    return $this->afterUpdate();
                }

                return true;
            }
        }

        return false;
    }
1059979
public function insert(array $values)
    {
        if (empty($values)) {
            return true;
        }
        $values = $this->detectValues($values);
        $bindings = [];
        foreach ($values as $record) {
            foreach ($record as $key => $value) {
                $bindings[$key] = $value;
            }
        }

        $sql = $this->grammar->compileInsert($this, $values);

        return $this->connection->insert($sql, $bindings);
    }
664172
public function upsert($table, $insertFieldValues, $updateFieldValues = [], $primaryKey = null)
    {
        if (!$primaryKey) {
            $primaryKey = (string)key($insertFieldValues);
        }

        if (!$updateFieldValues) {
            $updateFieldValues = $insertFieldValues;
        }

        $fields = array_keys($insertFieldValues);

        $bind = $insertFieldValues;
        $updates = [];
        foreach ($updateFieldValues as $k => $v) {
            $field = is_string($k) ? $k : $v;
            if ($primaryKey === $field) {
                continue;
            }

            if (is_int($k)) {
                $updates[] = "[$field]=:{$field}_dku";
                $bind["{$field}_dku"] = $insertFieldValues[$field];
            } elseif ($v instanceof AssignmentInterface) {
                $v->setFieldName($k);
                $updates[] = $v->getSql();
                /** @noinspection SlowArrayOperationsInLoopInspection */
                $bind = array_merge($bind, $v->getBind());
            } else {
                $updates[] = $v;
            }
        }

        $insertFieldsSql = '[' . implode('],[', $fields) . ']';
        $insertValuesSql = ':' . implode(',:', $fields);

        $updateFieldsSql = implode(',', $updates);

        $sql = 'INSERT' . " INTO {$this->_escapeIdentifier($table)}($insertFieldsSql) VALUES($insertValuesSql) ON DUPLICATE KEY UPDATE $updateFieldsSql";

        return $this->execute('insert', $sql, $bind);
    }
361507
public function replaceBatch(array $sets, array $where = [])
    {
        $this->where($where);
        $this->builderCache->store('sets', $sets);

        return $this->platformReplaceBatchHandler($this->builderCache);
    }
538999
final public function update(array $keys = null)
    {
        $pre = $this->preUpdate($keys);
        if ($pre === false) {
            $this->logger->error(sprintf(
                'Can not update object "%s:%s"; cancelled by %s::preUpdate()',
                $this->objType(),
                $this->id(),
                get_called_class()
            ));
            return false;
        }

        $ret = $this->source()->updateItem($this, $keys);
        if ($ret === false) {
            $this->logger->error(sprintf(
                'Can not update object "%s:%s"; repository failed for %s',
                $this->objType(),
                $this->id(),
                get_called_class()
            ));
            return false;
        }

        $post = $this->postUpdate($keys);
        if ($post === false) {
            $this->logger->warning(sprintf(
                'Updated object "%s:%s" but %s::postUpdate() failed',
                $this->objType(),
                $this->id(),
                get_called_class()
            ));
            return false;
        }

        return true;
    }
1052938
public function attach($keys): bool
    {
        if ($keys instanceof Arrayable) {
            $keys = $keys->toArray();
        }

        foreach ($keys as $key) {
            $this->add($key);
        }

        return true;
    }
550897
public function update($set = array())
    {
        if (is_array($set)) {
            $params = array();
            foreach ($set as $field => $param) {
                $this->add('set', $field . ' = ?', true);
                $params[] = $param;
            }
            $this->params = array_merge($params, $this->params);
        } else {
            $this->add('set', $set, true);
        }
        $this->type = self::UPDATE;
        return $this->execute();
    }
41745
public function insert(array $data = array(), $update = false)
    {
        $this->viewCheck(__FUNCTION__);

        $data = array_merge($this->_schema['init'],
            array_filter($this->_schema['values'], array($this, 'filterRule')),
            array_filter($this->_schema['others'], array($this, 'filterRule')),
            $data);

        $params = array();
        foreach ($data as $key => $value)
            if (in_array($key, $this->_properties['primary_key']) && !$value)
                continue;
            elseif (is_array($value))
                return false;
            elseif (isset($this->_properties['fields'][$key]))
                $params[':'.$key] = $value;

        if (empty($params) || ($update && !$this->_properties['primary_key']))
            return false;

        $query = $this->buildInsert($params, $update);

        if (!($query && $this->validate($params)))
            return false;

        $old = clone $this;
        if (!$this->beforeInsert($params))
            return false;

        if ($result = $this->run($query, $params))
            $this->assign($params);

        if (!$this->afterInsert($old))
            return false;

        return $result;
    }
1241397
public function insert(array $values): bool {
    $this->coerceDataTypesToDatabase($values);
    return $this->builder->insert($values);
  }
1324618
protected function updateInternal(array $fields = [])
    {
        $values = $this->getChangedAttributes($fields);
        if (empty($values)) {
            return true;
        }

        $rows = $this->objects()
            ->filter($this->getPrimaryKeyValues())
            ->update($values);

        foreach ($values as $name => $value) {
            $this->setAttribute($name, $value);
        }
        $this->updateRelated();

        return $rows >= 0;
    }