* @param DB_DataObject &$orig Must be "instanceof" $this
* @param string $pid Primary ID column (no escaping is done on column name!)
*/
- public function updateWithKeys(Managed_DataObject $orig, $pid='id')
+ public function updateWithKeys(Managed_DataObject $orig, $pid=null)
{
if (!$orig instanceof $this) {
throw new ServerException('Tried updating a DataObject with a different class than itself.');
}
+ if ($this->N <1) {
+ throw new ServerException('DataObject must be the result of a query (N>=1) before updateWithKeys()');
+ }
+
// do it in a transaction
$this->query('BEGIN');
return true;
}
- $qry = sprintf('UPDATE %1$s SET %2$s WHERE %3$s = %4$s',
+ if ($pid === null) {
+ $schema = static::schemaDef();
+ $pid = $schema['primary key'];
+ unset($schema);
+ }
+ $pidWhere = array();
+ foreach((array)$pid as $pidCol) {
+ $pidWhere[] = sprintf('%1$s = %2$s', $pidCol, $this->_quote($orig->$pidCol));
+ }
+ if (empty($pidWhere)) {
+ throw new ServerException('No primary ID column(s) set for updateWithKeys');
+ }
+
+ $qry = sprintf('UPDATE %1$s SET %2$s WHERE %3$s',
common_database_tablename($this->tableName()),
implode(', ', $parts),
- $pid,
- $this->_quote($orig->$pid));
+ implode(' AND ', $pidWhere));
$result = $this->query($qry);
if ($result === false) {
$orig = clone($this);
$this->callback = $httpscallback;
$this->hashkey = self::hashkey($this->getTopic(), $this->callback);
- $this->updateWithKeys($orig, 'hashkey');
+ $this->updateWithKeys($orig);
return true;
}
}
}
common_debug('URIFIX Updating Ostatus_profile URI for '.$orig->uri.' to '.$this->uri);
- $this->updateWithKeys($orig, 'uri'); // 'uri' is the primary key column
+ $this->updateWithKeys($orig); // Will use the PID column(s) in the 'UPDATE ... WHERE [unique selector]'
common_debug('URIFIX Subscribing/renewing feedsub for Ostatus_profile '.$this->uri);
$this->subscribe();
$thumbnail->width = $info[0]; // array indexes documented on php.net:
$thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
// Throws exception on failure.
- $thumbnail->updateWithKeys($orig, 'file_id');
+ $thumbnail->updateWithKeys($orig);
}
public function onPluginVersion(array &$versions)
$file->width = $info[0]; // array indexes documented on php.net:
$file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
// Throws exception on failure.
- $file->updateWithKeys($orig, 'id');
+ $file->updateWithKeys($orig);
}
// Get rid of the file from memory
unset($imgData);