Permalink
Browse files

adding options array to the save method to match the options in Mongo…

…Collection::save
  • Loading branch information...
1 parent 6a0c893 commit c49942f0391ce6065495db5c9a69e0d1bbd7a875 @a-musing-moose committed Apr 30, 2011
Showing with 25 additions and 7 deletions.
  1. +3 −2 src/morph/Object.php
  2. +22 −5 src/morph/Storage.php
View
@@ -198,11 +198,12 @@ public function __set($propertyName, $propertyValue)
/**
* Saves this object
*
+ * @param array $options Support the same options as MongoCollection::save()
* @return Morph_Object
*/
- public function save()
+ public function save(array $options = array())
{
- return Storage::instance()->save($this);
+ return Storage::instance()->save($this, $options);
}
/**
View
@@ -23,6 +23,8 @@ class Storage
* @var MongoDB
*/
private $db;
+
+ private $useSafe = false;
/**
* Returns the singleton instance of this class
@@ -67,6 +69,17 @@ private function __construct(\MongoDB $db)
{
$this->db = $db;
}
+
+ /**
+ * If set to true then the 'safe' option for saves is used
+ *
+ * @param boolean $useSafe
+ */
+ public function useSafe($useSafe)
+ {
+ $this->useSafe = (bool)$useSafe;
+ return $this;
+ }
/**
* Returns the associated MongoDB object
@@ -141,21 +154,25 @@ public function save(Object $object)
/**
* Inserts a new object into the database
*
- * @param Morph_Object $object
- * @return Morph_Object
+ * @param \morph\Object $object
+ * @param array $options
+ * @return \morph\Object
*/
- private function insert(Object $object)
+ private function insert(Object $object, array $options = array())
{
$data = $object->__getData();
//set an id if we do not have one
if(!\array_key_exists('_id', $data)){
$id = array(
- '_id' => \md5(\uniqid(\rand(), true))
+ '_id' => new \MongoId()
);
$data = \array_merge($id, $data);
}
- $savedOk = $this->db->selectCollection($object->collection())->save($data);
+
+ $options = array_merge(array('safe'=>$this->useSafe), $options);
+
+ $savedOk = $this->db->selectCollection($object->collection())->save($data, $options);
if($savedOk){
$object->__setData($data, Enum::STATE_CLEAN);
}

0 comments on commit c49942f

Please sign in to comment.