Solar_Sql_Adapter::update PHP Method

update() public method

Automatically applies [[Solar_Sql_Adapter::quote() | ]] to the data values for you.
public update ( string $table, array $data, string | array $where ) : integer
$table string The table to udpate.
$data array An associative array where the key is the column name and the value is the value to use for that column.
$where string | array The SQL WHERE clause to limit which rows are updated.
return integer The number of rows affected.
    public function update($table, $data, $where)
    {
        // the base statement
        $table = $this->quoteName($table);
        $stmt = "UPDATE {$table} SET ";
        // add "col = :col" pairs to the statement
        $tmp = array();
        foreach ($data as $col => $val) {
            $tmp[] = $this->quoteName($col) . " = :{$col}";
        }
        $stmt .= implode(', ', $tmp);
        // add the where clause
        if ($where) {
            $where = $this->quoteMulti($where, ' AND ');
            $where = $this->quoteNamesIn($where);
            $stmt .= " WHERE {$where}";
        }
        // execute the statement
        $result = $this->query($stmt, $data);
        return $result->rowCount();
    }

Usage Example

Exemplo n.º 1
0
 /**
  * 
  * Updates an existing session-data row in the database.
  * 
  * @param string $id The session ID.
  * 
  * @param string $data The serialized session data.
  * 
  * @return bool
  * 
  * @todo Should we log caught exceptions?
  *
  */
 protected function _update($id, $data)
 {
     $cols = array($this->_config['updated_col'] => date('Y-m-d H:i:s'), $this->_config['data_col'] => $data);
     $where = array("{$this->_config['id_col']} = ?" => $id);
     try {
         $this->_sql->update($this->_config['table'], $cols, $where);
         return true;
     } catch (Solar_Sql_Exception $e) {
         // @todo log this somehow?
         return false;
     }
 }
All Usage Examples Of Solar_Sql_Adapter::update