Skip to content
Browse files

Merge pull request #39 from MiniCodeMonkey/master

deleteByQuery(...) function was added, so its possible to delete multiple documents based on a query - Thanks MiniCodeMonkey
  • Loading branch information...
2 parents 66e3214 + 2aa5cae commit 90633e455cb7f8dc51577ab12216de94db73097c Jonathan Moss committed
Showing with 38 additions and 0 deletions.
  1. +12 −0 src/morph/Object.php
  2. +26 −0 src/morph/Storage.php
View
12 src/morph/Object.php
@@ -249,6 +249,18 @@ public function findByQuery(IQuery $query)
{
return Storage::instance()->findByQuery($this, $query);
}
+
+ /**
+ * Deletes objects by query
+ *
+ * @param IQuery $query
+ * @param bool $safe
+ * @return \morph\Iterator
+ */
+ public function deleteByQuery(IQuery $query, $safe = null)
+ {
+ return Storage::instance()->findByQuery($this, $query, $safe);
+ }
/**
* Finds one object by query
View
26 src/morph/Storage.php
@@ -207,6 +207,32 @@ public function delete(Object $object)
$query = array('_id' => $object->id());
return $this->db->selectCollection($object->collection())->remove($query, array('justOne' => true));
}
+
+ /**
+ * Deletes documents based on query
+ *
+ * The results come packages up in a \morph\Iterator object
+ *
+ * @param Object $object Required to determine the correct collection query against
+ * @param IQuery $query
+ * @param bool $safe
+ * @return bool
+ */
+ public function deleteByQuery(Object $object, IQuery $query = null, $safe = null)
+ {
+ $class = get_class($object);
+
+ $query = (is_null($query)) ? new Query() : $query;
+
+ $options = array();
+ if (!is_null($safe))
+ {
+ $options[] = array('safe' => $safe);
+ }
+
+ $rawQuery = $this->getRawQuery($object, $query);
+ return $this->db->selectCollection($object->collection())->remove($rawQuery, $options);
+ }
/**
* Runs query against the database

0 comments on commit 90633e4

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