aboutsummaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorMikael Nordfeldth <mmn@hethane.se>2016-01-28 16:42:59 +0100
committerMikael Nordfeldth <mmn@hethane.se>2016-01-28 16:42:59 +0100
commitefe23ed404bcdc65536aebab708bda93135ba903 (patch)
tree376e9019dddcf021ce68f262f23070e1f748acb3 /classes
parentd94f9031ff654343a3c6fd37924ce6cf144c66d7 (diff)
downloadgnu-social-efe23ed404bcdc65536aebab708bda93135ba903.tar
gnu-social-efe23ed404bcdc65536aebab708bda93135ba903.zip
updateWithKeys now understands multi-column keys
and automatically identifies _which_ columns are the right ones, so for example 'uri' primary keys don't need to be explicitly set
Diffstat (limited to 'classes')
-rw-r--r--classes/Managed_DataObject.php24
1 files changed, 20 insertions, 4 deletions
diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php
index 68de8922ce..c02685e55e 100644
--- a/classes/Managed_DataObject.php
+++ b/classes/Managed_DataObject.php
@@ -420,12 +420,16 @@ abstract class Managed_DataObject extends Memcached_DataObject
* @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');
@@ -452,11 +456,23 @@ abstract class Managed_DataObject extends Memcached_DataObject
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) {