From: Jan Schneider Date: Fri, 28 Aug 2009 22:47:16 +0000 (+0200) Subject: Return the new primary id if creating objects. We should really manipulate the existi... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=9ffa9a348780347ae0bb0b69abfffa699cff5702;p=horde.git Return the new primary id if creating objects. We should really manipulate the existing Rdo_Base object, if one is passed. --- diff --git a/framework/Rdo/lib/Horde/Rdo/Base.php b/framework/Rdo/lib/Horde/Rdo/Base.php index be8bfc606..1b3a19a2e 100644 --- a/framework/Rdo/lib/Horde/Rdo/Base.php +++ b/framework/Rdo/lib/Horde/Rdo/Base.php @@ -250,13 +250,14 @@ abstract class Horde_Rdo_Base implements IteratorAggregate { } /** - * Save any changes to the backend. + * Saves any changes to the backend. * - * @return boolean Success. + * @return boolean|integer Primary key if the object was created, whether + * the updated succeeded otherwise. */ public function save() { - return $this->getMapper()->update($this) == 1; + return $this->getMapper()->update($this); } /** diff --git a/framework/Rdo/lib/Horde/Rdo/Mapper.php b/framework/Rdo/lib/Horde/Rdo/Mapper.php index ac15e690d..b96d1626b 100644 --- a/framework/Rdo/lib/Horde/Rdo/Mapper.php +++ b/framework/Rdo/lib/Horde/Rdo/Mapper.php @@ -356,33 +356,33 @@ abstract class Horde_Rdo_Mapper implements Countable $id = $this->adapter->insert($sql, $bindParams); - return $this->map(array_merge(array($this->primaryKey => $id), - $fields)); + return $this->map(array_merge($fields, array($this->primaryKey => $id))); } /** - * Updates a record in the backend. $object can be either a - * primary key or an Rdo object. If $object is an Rdo instance - * then $fields will be ignored as values will be pulled from the - * object. + * Updates a record in the backend. * - * @param string|Rdo $object The Rdo instance or unique id to update. - * @param array $fields If passing a unique id, the array of field properties - * to set for $object. + * $object can be either a primary key or an Rdo object. If $object is an + * Rdo instance then $fields will be ignored as values will be pulled from + * the object. * - * @return integer Number of objects updated. + * @param string|Rdo $object The Rdo instance or unique id to update. + * @param array $fields If passing a unique id, the array of field + * properties to set for $object. + * + * @return boolean|integer Primary key if the object was created, whether + * the updated succeeded otherwise. */ public function update($object, $fields = null) { if ($object instanceof Horde_Rdo_Base) { - $key = $this->primaryKey; - $id = $object->$key; + $id = $object->{$this->primaryKey}; $fields = iterator_to_array($object); if (!$id) { // Object doesn't exist yet; create it instead. $object = $this->create($fields); - return 1; + return $object->{$this->primaryKey}; } } else { $id = $object; @@ -410,7 +410,7 @@ abstract class Horde_Rdo_Mapper implements Countable $sql = substr($sql, 0, -1) . ' WHERE ' . $this->primaryKey . ' = ?'; $bindParams[] = $id; - return $this->adapter->update($sql, $bindParams); + return (bool)$this->adapter->update($sql, $bindParams); } /**