public function save() { $data = sDB::prepare($this, $this->_map); $id = $this->{$this->_key}; if (!empty($this->_map)) { $remap = array_flip($this->_map); if (isset($remap[$this->_key])) { $id = $this->{$remap[$this->_key]}; } } // Insert new record $data['modified'] = "'" . current_time('mysql') . "'"; $dataset = ShoppDatabaseObject::dataset($data); $query = "INSERT {$this->_table} SET {$dataset} ON DUPLICATE KEY UPDATE {$dataset}"; $id = sDB::query($query); do_action_ref_array('shopp_save_productsummary', array(&$this)); return $id; }
/** * Saves the current state of the ShoppDatabaseObject to the database * * Intelligently saves a ShoppDatabaseObject, using an UPDATE query when the * value for the primary key is set, and using an INSERT query when the * value of the primary key is not set. * * @author Jonathan Davis * @since 1.0 * @version 1.2 * * @return boolean|int Returns true when UPDATEs are successful; returns an integer with the record ID **/ public function save() { $classhook = strtolower(get_class($this)); $data = sDB::prepare($this, $this->_map); $id = isset($this->{$this->_key}) ? $this->{$this->_key} : false; if (!empty($this->_map)) { $remap = array_flip($this->_map); if (isset($remap[$this->_key])) { $id = $this->{$remap[$this->_key]}; } } $time = current_time('mysql'); if (isset($data['modified'])) { $data['modified'] = "'{$time}'"; } if (empty($id)) { // Insert new record if (isset($data['created'])) { $data['created'] = "'{$time}'"; } $dataset = ShoppDatabaseObject::dataset($data); $this->id = sDB::query("INSERT {$this->_table} SET {$dataset}"); do_action_ref_array("shopp_save_{$classhook}", array($this)); do_action_ref_array("shopp_create_{$classhook}", array($this)); return $this->id; } // Update record $dataset = ShoppDatabaseObject::dataset($data); sDB::query("UPDATE {$this->_table} SET {$dataset} WHERE {$this->_key}='{$id}'"); do_action_ref_array("shopp_save_{$classhook}", array($this)); return true; }
/** * Updates the setting in the registry and the database * * @since 1.0 * * @param string $name Name of the setting * @param mixed $value Value of the setting to update * @return boolean **/ public function update($name, $value) { if ($this->get($name) === $value) { return true; } $Setting = $this->setting(); $Setting->name = $name; $Setting->value = sDB::clean($value); $data = sDB::prepare($Setting); // Prepare the data for db entry $dataset = ShoppDatabaseObject::dataset($data); // Format the data in SQL $where = array("context='{$Setting->context}'", "type='{$Setting->type}'"); if (!empty($name)) { $where[] = "name='" . sDB::clean($name) . "'"; } $where = join(' AND ', $where); if (sDB::query("UPDATE {$this->_table} SET {$dataset} WHERE {$where}")) { $this->registry[$name] = $this->restore($value); } else { return false; } return true; }