public function update($return_affected = false) { PHPWS_DB::touchDB(); $table = $this->getTable(true); if (!$table) { return PHPWS_Error::get(PHPWS_DB_ERROR_TABLE, 'core', 'PHPWS_DB::insert'); } $values = $this->getAllValues(); $where = $this->getWhere(true); if (!empty($where)) { $where = 'WHERE ' . $where; } if (empty($values)) { return PHPWS_Error::get(PHPWS_DB_NO_VALUES, 'core', 'PHPWS_DB::update'); } foreach ($values as $index => $data) { $columns[] = $index . ' = ' . PHPWS_DB::dbReady($data); } $limit = $this->getLimit(true); $order = $this->getOrder(true); $query = "UPDATE {$table} SET " . implode(', ', $columns) . " {$where} {$order} {$limit}"; $result = PHPWS_DB::query($query); if (DB::isError($result)) { return $result; } else { if ($return_affected) { return $this->affectedRows(); } else { return true; } } }
/** * Prepares a value for database writing or reading * * @author Matt McNaney <matt at NOSPAM dot tux dot appstate dot edu> * @param mixed $value The value to prepare for the database. * @return mixed $value The prepared value * @access public */ public function dbReady($value = null) { if (is_array($value) || is_object($value)) { return PHPWS_DB::dbReady(serialize($value)); } elseif (is_string($value)) { return "'" . $GLOBALS['PHPWS_DB']['connection']->escape($value) . "'"; } elseif (is_null($value)) { return 'NULL'; } elseif (is_bool($value)) { return $value ? 1 : 0; } else { return $value; } }