/** * Saves the data into the database, checking whether is a new row or an old * one that just needs an update * @return boolean */ public function save() { if (!$this->id) { $fields = array_keys($this->_data); $fieldsString = implode(', ', $fields); $values = array_values($this->_data); $aux = array(); foreach ($values as $value) { $aux[] = "'" . mysql_escape_string($value) . "'"; } $values = implode(', ', $aux); $sql = "INSERT INTO\n {$this->table_name}({$fieldsString})\n VALUES({$values});"; if (!$this->_DbConnection->executeQuery($sql)) { return false; } $this->_id = $this->_DbConnection->getLastId(); $this->_data[$this->_idField] = $this->_id; } else { $fieldsStrings = array(); foreach ($this->_data as $field => $value) { $fieldsStrings[] = "{$field}='" . mysql_escape_string($value) . "'"; } $fieldString = implode(', ', $fieldsStrings); $sql = "UPDATE {$this->_tableName}\n SET {$fieldString}\n WHERE {$this->_idField}={$this->_id}\n LIMIT 1;"; if (!$this->_DbConnection->executeQuery($sql)) { return false; } } if (!$this->_loaded) { $this->_loaded = true; } return true; }
/** * Saves the data into the database, checking whether is a new row or an old * one that just needs an update * @return boolean */ public function save() { if (!$this->_id) { $fields = array_keys($this->_data); $fieldsString = implode(', ', $fields); $values = array_values($this->_data); $aux = array(); foreach ($values as $value) { $aux[] = "'" . mysql_escape_string($value) . "'"; } $values = implode(', ', $aux); $sql = "INSERT INTO\n {$this->_tableName}({$fieldsString})\n VALUES({$values});"; try { $this->_DbConnection->execute($sql); } catch (RuntimeException $Exception) { Logger::log('Couln\'t save Row', $Exception->getMessage(), 'error'); return false; } $this->_id = $this->_DbConnection->getLastId(); $this->_data[$this->_idField] = $this->_id; } else { $fieldsStrings = array(); foreach ($this->_data as $field => $value) { $fieldsStrings[] = "{$field}='" . mysql_escape_string($value) . "'"; } $fieldString = implode(', ', $fieldsStrings); $sql = "UPDATE {$this->_tableName}\n SET {$fieldString}\n WHERE {$this->_idField}={$this->_id}\n LIMIT 1;"; if (!$this->_DbConnection->execute($sql)) { return false; } } if (!$this->_loaded) { $this->_loaded = true; } return true; }