/** * Creates and executes an UPDATE SQL statement. * * @param string $table table * @param array $columns array of 'column'=>'value' entries * @param mixed $criteria string where clause or object criteria * * @return boolean|null true if no errors, false if errors encountered */ public function update($table, $columns, $criteria) { if (isset($this->tables[$table])) { $tableDef =& $this->tables[$table]; $where = ''; if (is_scalar($criteria)) { $where = 'WHERE ' . $criteria; } elseif (is_object($criteria)) { $where = $criteria->renderWhere(); } $colsql = ''; foreach ($tableDef['columns'] as $col) { $comma = empty($colsql) ? '' : ', '; if (isset($columns[$col['name']])) { $colsql .= $comma . $col['name'] . ' = ' . $this->db->quote($columns[$col['name']]); } } $sql = "UPDATE `{$tableDef['name']}` SET {$colsql} {$where}"; $this->queue[] = $sql; return true; } else { // no table established $this->lastError = _DB_XMF_TABLE_IS_NOT_DEFINED; $this->lastErrNo = -1; return null; } }
/** * Create an UPDATE SQL statement and add it to the work queue * * @param string $table table * @param array $columns array of 'column'=>'value' entries * @param string|CriteriaElement $criteria string where clause or object criteria * @param boolean $quoteValue true to quote values, false if caller handles quoting * * @return boolean true if no errors, false if errors encountered */ public function update($table, $columns, $criteria, $quoteValue = true) { if (isset($this->tables[$table])) { $tableDef = $this->tables[$table]; $where = ''; if (is_scalar($criteria)) { $where = $criteria; } elseif (is_object($criteria)) { $where = $criteria->renderWhere(); } $colSql = ''; foreach ($tableDef['columns'] as $col) { $comma = empty($colSql) ? '' : ', '; if (isset($columns[$col['name']])) { $colSql .= "{$comma}`{$col['name']}` = " . ($quoteValue ? $this->db->quote($columns[$col['name']]) : $columns[$col['name']]); } } $sql = "UPDATE `{$tableDef['name']}` SET {$colSql} {$where}"; $this->queue[] = $sql; return true; } else { return $this->tableNotEstablished(); } }