Basecoat\DB::update PHP Метод

update() публичный Метод

Update record(s) in database
public update ( string $tablename, array $data, string $filter, string $filterBindings = null, boolean $useMaster = true ) : integer
$tablename string name of table to update
$data array associative array of field/value pairs to update. Note only raw data is supported, not formulas
$filter string SQL filter to apply to update, should be properly escaped
$filterBindings string associative array name/value pairs to use for binding. CAUTION: may override $data bindings if same names are used
$useMaster boolean set to TRUE to use the master server connection
Результат integer number of rows updated, or negative if an error occurred
    public function update($tablename, $data, $filter, $filterBindings = null, $useMaster = true)
    {
        // Check for filter bindings
        if (is_array($filterBindings)) {
            // Check if there are any binding conflicts between $data and $filterBindings
            $conflicts = array_intersect_key($data, $filterBindings);
            if (count($conflicts) > 0) {
                $this->errorCode = 'n/a';
                $this->errorMsg = 'Duplicate bindings for data and filter (' . implode(', ', array_keys($conflicts)) . ').';
                return -2;
            }
        } elseif (!is_null($filterBindings)) {
            // Invalid filter binding specification, must be an array or null
            $this->errorCode = 'n/a';
            $this->errorMsg = 'Invalid filter binding passed, must be an array or null.';
            return -3;
        }
        // Add backticks to table name
        $tablename = '`' . str_replace('.', '`.`', trim($tablename, '"\'`')) . '`';
        $query = 'UPDATE ' . $tablename . ' SET ';
        $setList = array();
        // Build "set" update string and Add proper name binding prefix to binding array
        $setList = array();
        foreach ($data as $field => $val) {
            $setList[] = '`' . $field . '`= :' . $field;
        }
        $query .= implode(', ', $setList) . ' WHERE ' . $filter;
        // Check if we need to add filter bindings
        if (is_array($filterBindings)) {
            $data = array_merge($data, $filterBindings);
        }
        $this->prepare($query, $useMaster);
        return $this->execute($data, $useMaster);
    }