Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added deleteByQuery(...) function, so its possible to delete multiple…

… documents based on a query
  • Loading branch information...
commit 2aa5cae8b0634247d3eb3445f306591badd70725 1 parent 66e3214
@MiniCodeMonkey MiniCodeMonkey authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.