*/
public function __construct($fields = array())
{
- $this->_fields = $fields;
+ $this->setFields($fields);
}
/**
}
/**
+ * 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.
*
$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) {
}
}
- $o = new $this->_classname($fields);
- $o->setMapper($this);
+ $object->setFields($fields);
if (count($relationships)) {
foreach ($this->relationships as $relationship => $rel) {
}
}
}
-
- if (is_callable(array($o, 'afterMap'))) {
- $o->afterMap();
- }
-
- return $o;
}
/**
$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)));
}
/**
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 {