From c6597d52b27e9a8475f91ecce0339ff029256fde Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Wed, 2 Sep 2009 23:29:52 -0400 Subject: [PATCH] Refactor some of the Rdo Base and Mapper objects to allow updating an existing object on save(). --- framework/Rdo/lib/Horde/Rdo/Base.php | 14 +++++++++++++- framework/Rdo/lib/Horde/Rdo/Mapper.php | 35 +++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/framework/Rdo/lib/Horde/Rdo/Base.php b/framework/Rdo/lib/Horde/Rdo/Base.php index be8bfc606..3d4c5c15f 100644 --- a/framework/Rdo/lib/Horde/Rdo/Base.php +++ b/framework/Rdo/lib/Horde/Rdo/Base.php @@ -40,7 +40,7 @@ abstract class Horde_Rdo_Base implements IteratorAggregate { */ public function __construct($fields = array()) { - $this->_fields = $fields; + $this->setFields($fields); } /** @@ -191,6 +191,18 @@ abstract class Horde_Rdo_Base implements IteratorAggregate { } /** + * Set field values for the object + * + * @param array $fields Initial values for the new object. + * + * @see Horde_Rdo_Mapper::map() + */ + public function setFields($fields = array()) + { + $this->_fields = $fields; + } + + /** * Implement the IteratorAggregate interface. Looping over an Rdo * object goes through each property of the object in turn. * diff --git a/framework/Rdo/lib/Horde/Rdo/Mapper.php b/framework/Rdo/lib/Horde/Rdo/Mapper.php index ac15e690d..9c6f15156 100644 --- a/framework/Rdo/lib/Horde/Rdo/Mapper.php +++ b/framework/Rdo/lib/Horde/Rdo/Mapper.php @@ -202,6 +202,26 @@ abstract class Horde_Rdo_Mapper implements Countable $this->_classname = $this->mapperToEntity(); } + $o = new $this->_classname(); + $o->setMapper($this); + + $this->mapFields($o, $fields); + + if (is_callable(array($o, 'afterMap'))) { + $o->afterMap(); + } + + return $o; + } + + /** + * Update an instance of $this->_classname from a set of data. + * + * @param Horde_Rdo_Base $object The object to update + * @param array $fields Field names/default values for the object + */ + public function mapFields($object, $fields = array()) + { $relationships = array(); foreach ($fields as $fieldName => &$fieldValue) { if (strpos($fieldName, '@') !== false) { @@ -218,8 +238,7 @@ abstract class Horde_Rdo_Mapper implements Countable } } - $o = new $this->_classname($fields); - $o->setMapper($this); + $object->setFields($fields); if (count($relationships)) { foreach ($this->relationships as $relationship => $rel) { @@ -238,12 +257,6 @@ abstract class Horde_Rdo_Mapper implements Countable } } } - - if (is_callable(array($o, 'afterMap'))) { - $o->afterMap(); - } - - return $o; } /** @@ -356,8 +369,7 @@ 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))); } /** @@ -381,7 +393,8 @@ abstract class Horde_Rdo_Mapper implements Countable if (!$id) { // Object doesn't exist yet; create it instead. - $object = $this->create($fields); + $o = $this->create($fields); + $this->mapFields($object, iterator_to_array($o)); return 1; } } else { -- 2.11.0