Feature/save only changes #41

Merged
merged 3 commits into from Aug 16, 2012
Jump to file or symbol
Failed to load files and symbols.
+65 −18
Split
View
@@ -129,24 +129,37 @@ public function __setData(array $data, $state = Enum::STATE_DIRTY)
/**
* Gets the property data for this object
*
+ * @param boolean $whereNew Whether or not to only get dirty/new properties.
* @return array
*/
- public function __getData()
+ public function __getData($whereNew = false)
{
$data = array();
if (!is_null($this->id)) {
$data['_id'] = $this->id;
}
$data['_ns'] = \get_class($this);
- foreach($this->propertySet as $property) {
- $storageName = $this->propertySet->getStorageName($property->getName());
- $data[$storageName] = $property->__getRawValue();
+ foreach ($this->propertySet as $property) {
+ $storageName = $this->propertySet->getStorageName($property->getName());
+
+ if (!$whereNew) {
+ $data[$storageName] = $property->__getRawValue();
+ } else {
+ $state = $property->getState();
+
+ if ($state === \morph\Enum::STATE_DIRTY || $state === \morph\Enum::STATE_NEW) {
+ $data[$storageName] = $property->__getRawValue();
+ }
+ }
}
+
return $data;
}
/**
- * @return \morph\PropertSet
+ * Returns the PropertySet for this object
+ *
+ * @return \morph\PropertySet
*/
public function __getPropertySet()
{
@@ -158,6 +171,8 @@ public function __getPropertySet()
// ********************** //
/**
+ * Gets a property by name if it exists. Issues an E_USER_WARNING if it does not exist.
+ *
* @param $propertyName
* @return mixed
*/
@@ -173,6 +188,8 @@ public function __get($propertyName)
}
/**
+ * Sets a property by name and value
+ *
* @param string $propertyName
* @param string $propertyValue
* @return \morph\Object
View
@@ -77,7 +77,12 @@ public function __setRawPropertyValue($name, $value, $state = null)
}
$this[$name]->__setRawValue($value, $state);
}
-
+
+ /**
+ * Gets the current state for the entire property set
+ *
+ * @return string A string that matches to a constant in \Morph\Enum
+ */
public function getState()
{
$state = \morph\Enum::STATE_NEW;
@@ -104,7 +109,7 @@ public function getState()
return $state;
}
-
+
/**
* Sets a storage alias for the named property
*
@@ -156,7 +161,7 @@ private function reverseStorageName($storageName)
*/
public function append($object)
{
- throw new \RuntimeException("Appending to PropertySet is not supported");
+ throw new \RuntimeException("Appending to morph\\PropertySet is not supported");
}
/**
@@ -184,7 +189,7 @@ public function offsetSet($offset, $object)
private function checkType($object)
{
if (!\is_object($object)) {
- throw new \InvalidArgumentException('value if not and object that extends Morph_Property_Generic');
+ throw new \InvalidArgumentException('value if not and object that extends morph\\property\\Generic');
}
if(!($object instanceof \morph\property\Generic)){
View
@@ -160,8 +160,8 @@ public function save(Object $object)
/**
* Inserts a new object into the database
*
- * @param \morph\Object $object
- * @param array $options
+ * @param \morph\Object $object
+ * @param array $options
* @return \morph\Object
*/
private function insert(Object $object, array $options = array())
@@ -174,7 +174,9 @@ private function insert(Object $object, array $options = array())
'_id' => new \MongoId()
);
$data = \array_merge($id, $data);
- }
+ } else {
+ throw new \InvalidArgumentException('Cannot insert an object with an id already');
+ }
$options = array_merge(array('safe'=>$this->useSafe), $options);
@@ -188,18 +190,41 @@ private function insert(Object $object, array $options = array())
/**
* Updates object in the database
*
- * @param Morph_Object $object
- * @return Morph_Object
+ * @param morph\Object $object
+ * @param array $options
+ * @return morph\Object
*/
- private function update(Object $object)
+ private function update(Object $object, array $options = array())
{
- return $this->insert($object);
+ // get "dirty" or "new" data
+ $data = $object->__getData(TRUE);
+
+ // make sure we have an id
+ if(is_null($object->id())) {
+ throw new \InvalidArgumentException("Cannot update an object that has yet to be inserted");
+ } else {
+ // remove the id from the $set process
+ unset($data['_id']);
+ }
+
+ // criteria can generally be the id of the document
+ $query = array('_id' => $object->id());
+
+ $options = array_merge(array('safe' => $this->useSafe, 'multiple' => false), $options);
+
+ $data = array('$set' => $data);
+
+ $savedOk = $this->db->selectCollection($object->collection())->update($query, $data, $options);
+ if($savedOk) {
+ $object->__setData($data, Enum::STATE_CLEAN);
+ }
+ return $object;
}
/**
* Deletes the object passed in from the database
- * @param Morph_Object $object
+ * @param morph\Object $object
* @return boolean
*/
public function delete(Object $object)
@@ -215,7 +240,7 @@ public function delete(Object $object)
*
* @param Object $object Required to determine the correct collection query against
* @param IQuery $query
- * @param bool $safe
+ * @param bool $safe
* @return bool
*/
public function deleteByQuery(Object $object, IQuery $query = null, $safe = null)