/** * DB_DataObject doesn't allow updating keys (even non-primary) */ function updateKeys(&$orig) { $this->_connect(); foreach (array('hostname', 'pathname') as $k) { if (strcmp($this->{$k}, $orig->{$k}) != 0) { $parts[] = $k . ' = ' . $this->_quote($this->{$k}); } } if (count($parts) == 0) { // No changes return true; } $toupdate = implode(', ', $parts); $table = common_database_tablename($this->tableName()); $qry = 'UPDATE ' . $table . ' SET ' . $toupdate . ' WHERE nickname = ' . $this->_quote($this->nickname); $orig->decache(); $result = $this->query($qry); if ($result) { $this->encache(); } return $result; }
/** * @param DB_DataObject &$orig Must be "instanceof" $this * @param string $pid Primary ID column (no escaping is done on column name!) */ public function updateWithKeys(&$orig, $pid = 'id') { if (!$orig instanceof $this) { throw new ServerException('Tried updating a DataObject with a different class than itself.'); } // do it in a transaction $this->query('BEGIN'); $parts = array(); foreach ($this->keys() as $k) { if (strcmp($this->{$k}, $orig->{$k}) != 0) { $parts[] = $k . ' = ' . $this->_quote($this->{$k}); } } if (count($parts) == 0) { // No changes to keys, it's safe to run ->update(...) if ($this->update($orig) === false) { common_log_db_error($this, 'UPDATE', __FILE__); // rollback as something bad occurred $this->query('ROLLBACK'); throw new ServerException("Could not UPDATE non-keys for {$this->tableName()}"); } $orig->decache(); $this->encache(); // commit our db transaction since we won't reach the COMMIT below $this->query('COMMIT'); // @FIXME return true only if something changed (otherwise 0) return true; } $qry = sprintf('UPDATE %1$s SET %2$s WHERE %3$s = %4$s', common_database_tablename($this->tableName()), implode(', ', $parts), $pid, $this->_quote($this->{$pid})); $result = $this->query($qry); if ($result === false) { common_log_db_error($this, 'UPDATE', __FILE__); // rollback as something bad occurred $this->query('ROLLBACK'); throw new ServerException("Could not UPDATE key fields for {$this->tableName()}"); } // Update non-keys too, if the previous endeavour worked. // The ->update call uses "$this" values for keys, that's why we can't do this until // the keys are updated (because they might differ from $orig and update the wrong entries). if ($this->update($orig) === false) { common_log_db_error($this, 'UPDATE', __FILE__); // rollback as something bad occurred $this->query('ROLLBACK'); throw new ServerException("Could not UPDATE non-keys for {$this->tableName()}"); } $orig->decache(); $this->encache(); // commit our db transaction $this->query('COMMIT'); // @FIXME return true only if something changed (otherwise 0) return $result; }
function updateKeys(&$orig) { $this->_connect(); $parts = array(); foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) { if (strcmp($this->{$k}, $orig->{$k}) != 0) { $parts[] = $k . ' = ' . $this->_quote($this->{$k}); } } if (count($parts) == 0) { // No changes return true; } $toupdate = implode(', ', $parts); $table = common_database_tablename($this->tableName()); $qry = 'UPDATE ' . $table . ' SET ' . $toupdate . ' WHERE id = ' . $this->id; $orig->decache(); $result = $this->query($qry); if ($result) { $this->encache(); } return $result; }
function showContent() { // XXX: Note I'm doing it this two-stage way because a raw query // with a JOIN was *not* working. --Zach $featured_nicks = common_config('nickname', 'featured'); if (count($featured_nicks) > 0) { $quoted = array(); foreach ($featured_nicks as $nick) { $quoted[] = "'{$nick}'"; } $user = new User(); $user->whereAdd(sprintf('nickname IN (%s)', implode(',', $quoted))); $user->limit(($this->page - 1) * PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1); $user->orderBy(common_database_tablename('user') . '.nickname ASC'); $user->find(); $profile_ids = array(); while ($user->fetch()) { $profile_ids[] = $user->id; } $profile = new Profile(); $profile->whereAdd(sprintf('profile.id IN (%s)', implode(',', $profile_ids))); $profile->orderBy('nickname ASC'); $cnt = $profile->find(); if ($cnt > 0) { $featured = new ProfileList($profile, $this); $featured->show(); } $profile->free(); $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE, $this->page, 'featured'); } }