Skip to content

Commit

Permalink
adding options array to the save method to match the options in Mongo…
Browse files Browse the repository at this point in the history
…Collection::save
  • Loading branch information
a-musing-moose committed Apr 30, 2011
1 parent 6a0c893 commit c49942f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/morph/Object.php
Expand Up @@ -198,11 +198,12 @@ public function __set($propertyName, $propertyValue)
/** /**
* Saves this object * Saves this object
* *
* @param array $options Support the same options as MongoCollection::save()
* @return Morph_Object * @return Morph_Object
*/ */
public function save() public function save(array $options = array())
{ {
return Storage::instance()->save($this); return Storage::instance()->save($this, $options);
} }


/** /**
Expand Down
27 changes: 22 additions & 5 deletions src/morph/Storage.php
Expand Up @@ -23,6 +23,8 @@ class Storage
* @var MongoDB * @var MongoDB
*/ */
private $db; private $db;

private $useSafe = false;


/** /**
* Returns the singleton instance of this class * Returns the singleton instance of this class
Expand Down Expand Up @@ -67,6 +69,17 @@ private function __construct(\MongoDB $db)
{ {
$this->db = $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 * Returns the associated MongoDB object
Expand Down Expand Up @@ -141,21 +154,25 @@ public function save(Object $object)
/** /**
* Inserts a new object into the database * Inserts a new object into the database
* *
* @param Morph_Object $object * @param \morph\Object $object
* @return Morph_Object * @param array $options
* @return \morph\Object
*/ */
private function insert(Object $object) private function insert(Object $object, array $options = array())
{ {
$data = $object->__getData(); $data = $object->__getData();


//set an id if we do not have one //set an id if we do not have one
if(!\array_key_exists('_id', $data)){ if(!\array_key_exists('_id', $data)){
$id = array( $id = array(
'_id' => \md5(\uniqid(\rand(), true)) '_id' => new \MongoId()
); );
$data = \array_merge($id, $data); $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){ if($savedOk){
$object->__setData($data, Enum::STATE_CLEAN); $object->__setData($data, Enum::STATE_CLEAN);
} }
Expand Down

0 comments on commit c49942f

Please sign in to comment.