Skip to content
Browse files

[FEATURE] Save only changes on update using

  • Loading branch information...
1 parent 2e24a7b commit 34ac11dc39d57db3541d179150684a75b27e4b82 chrsm committed
Showing with 11 additions and 10 deletions.
  1. +2 −1 src/morph/Object.php
  2. +9 −9 src/morph/Storage.php
View
3 src/morph/Object.php
@@ -129,6 +129,7 @@ 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($whereNew = false)
@@ -151,7 +152,7 @@ public function __getData($whereNew = false)
}
}
}
- var_dump($data);
+
return $data;
}
View
18 src/morph/Storage.php
@@ -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);
@@ -194,25 +196,23 @@ private function insert(Object $object, array $options = array())
*/
private function update(Object $object, array $options = array())
{
- // get changed data (\morph\Enum::STATE_DIRTY || \morph\Enum::STATE_NEW)
+ // get "dirty" or "new" data
$data = $object->__getData(TRUE);
- //set an id if we do not have one
- if(!\array_key_exists('_id', $data)){
- throw new \Exception('Can\'t call update on an object that hasn\'t been saved');
+ // 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 {
- // now that we know it had an id to begin with,
- // we need to remove it - we can't make changes to an id property.
+ // 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' => true, 'multiple' => false), $options);
+ $options = array_merge(array('safe' => $this->useSafe, 'multiple' => false), $options);
$data = array('$set' => $data);
- var_dump($data);
$savedOk = $this->db->selectCollection($object->collection())->update($query, $data, $options);
if($savedOk) {

0 comments on commit 34ac11d

Please sign in to comment.
Something went wrong with that request. Please try again.