ManaPHP\Db::update PHP Method

update() public method

Updates data on a table using custom SQL syntax Updating existing robot $success = $connection->update( "robots", array("name"), array("New Boy"), "id = 101" ); Next SQL sentence is sent to the database system UPDATE robots SET name = "boy" WHERE id = 101
public update ( string $table, array $columnValues, string | array $conditions, array $bind = [] ) : integer
$table string
$columnValues array
$conditions string | array
$bind array
return integer
    public function update($table, $columnValues, $conditions, $bind = [])
    {
        $escapedTable = "`{$table}`";
        if (count($columnValues) === 0) {
            throw new DbException('Unable to update :table table without data', ['table' => $table]);
        }
        if (is_string($conditions)) {
            $conditions = [$conditions];
        }
        $wheres = [];
        /** @noinspection ForeachSourceInspection */
        foreach ($conditions as $k => $v) {
            if (is_int($k)) {
                $wheres[] = Text::contains($v, ' or ', true) ? "({$v})" : $v;
            } else {
                $wheres[] = "`{$k}`=:{$k}";
                $bind[$k] = $v;
            }
        }
        $setColumns = [];
        foreach ($columnValues as $k => $v) {
            if (is_int($k)) {
                $setColumns[] = $v;
            } else {
                $setColumns[] = "`{$k}`=:{$k}";
                $bind[$k] = $v;
            }
        }
        $updateColumns = implode(',', $setColumns);
        $updateSql = "UPDATE {$escapedTable} SET {$updateColumns} WHERE " . implode(' AND ', $wheres);
        return $this->execute($updateSql, $bind);
    }