PDOWrapper::update PHP Метод

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

- updates a row to the specified table
public update ( string $table, array $params, array $wheres = [], boolean $timestamp_this = null ) : integer | boolean
$table string - the name of the db table we are adding row to
$params array - associative array representing the columns and their respective values to update
$wheres array (Optional) - the where clause of the query
$timestamp_this boolean (Optional) - if true we set date_created and date_modified values to now
Результат integer | boolean - the amount of rows updated, false on failure
    public function update($table, $params, $wheres = array(), $timestamp_this = null)
    {
        if (is_null($timestamp_this)) {
            $timestamp_this = self::$TIMESTAMP_WRITES;
        }
        // build the set part of the update query by
        // adding each parameter into the set query string
        $add_comma = false;
        $set_string = '';
        foreach ($params as $key => $val) {
            // only add comma after the first parameter has been appended
            if ($add_comma) {
                $set_string .= ', ';
            } else {
                $add_comma = true;
            }
            // now append the parameter
            $set_string .= "{$key}=:param_{$key}";
        }
        // add the timestamp columns if neccessary
        if ($timestamp_this === true) {
            $set_string .= ($add_comma ? ', ' : '') . 'date_modified=' . time();
        }
        // lets add our where clause if we have one
        $where_string = '';
        if (!empty($wheres)) {
            // load each key value pair, and implode them with an AND
            $where_array = array();
            foreach ($wheres as $key => $val) {
                $where_array[] = "{$key}=:where_{$key}";
            }
            // build the final where string
            $where_string = 'WHERE ' . implode(' AND ', $where_array);
        }
        // build final update string
        $sql_str = "UPDATE {$table} SET {$set_string} {$where_string}";
        // now we attempt to write this row into the database
        try {
            $pstmt = $this->getMaster()->prepare($sql_str);
            // bind each parameter in the array
            foreach ($params as $key => $val) {
                $pstmt->bindValue(':param_' . $key, $val);
            }
            // bind each where item in the array
            foreach ($wheres as $key => $val) {
                $pstmt->bindValue(':where_' . $key, $val);
            }
            // execute the update query
            $successful_update = $pstmt->execute();
            // if we were successful, return the amount of rows updated, otherwise return false
            return $successful_update == true ? $pstmt->rowCount() : false;
        } catch (PDOException $e) {
            if (self::$LOG_ERRORS == true) {
                error_log('DATABASE WRAPPER::' . print_r($e, true));
            }
            $this->pdo_exception = $e;
            return false;
        } catch (Exception $e) {
            if (self::$LOG_ERRORS == true) {
                error_log('DATABASE WRAPPER::' . print_r($e, true));
            }
            $this->pdo_exception = $e;
            return false;
        }
    }