updateWithKeys reworked to more reliable execution

Throws exception on UPDATE fails.
This commit is contained in:
Mikael Nordfeldth 2015-01-25 12:29:28 +01:00
parent 4917a422a1
commit 998afe1844

View File

@ -329,8 +329,8 @@ abstract class Managed_DataObject extends Memcached_DataObject
throw new ServerException('Tried updating a DataObject with a different class than itself.'); throw new ServerException('Tried updating a DataObject with a different class than itself.');
} }
// Update non-keys first, if necessary. // do it in a transaction
$this->update($orig); $this->query('BEGIN');
$parts = array(); $parts = array();
foreach ($this->keys() as $k) { foreach ($this->keys() as $k) {
@ -339,7 +339,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
} }
} }
if (count($parts) == 0) { if (count($parts) == 0) {
// No changes // No changes, unless made in the ->update call
return true; return true;
} }
$toupdate = implode(', ', $parts); $toupdate = implode(', ', $parts);
@ -349,9 +349,24 @@ abstract class Managed_DataObject extends Memcached_DataObject
' WHERE id = ' . $this->getID(); ' WHERE id = ' . $this->getID();
$orig->decache(); $orig->decache();
$result = $this->query($qry); $result = $this->query($qry);
if ($result !== false) { if ($result === false) {
$this->encache(); 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->__table}");
} }
// Update non-keys too, if the previous endeavour worked.
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->__table}");
}
$this->encache();
// commit our db transaction
$this->query('COMMIT');
return $result; return $result;
} }
} }