Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding a fetchByObjectId function

  • Loading branch information...
commit 7713f90c8cb533200b0e0367b8ba7e179dec05e1 1 parent d6845d7
Jonthan Moss authored
Showing with 69 additions and 1 deletion.
  1. +1 −1  make_phar.php
  2. +22 −0 src/morph/Object.php
  3. +46 −0 src/morph/Storage.php
View
2  make_phar.php
@@ -27,4 +27,4 @@
$phar->setStub(file_get_contents($src_dir . '..' . DIRECTORY_SEPARATOR . 'bootstrap.php'));
$phar->setMetadata($metadata);
$phar->compressFiles(Phar::GZ);
-echo "All done!";
+echo "All done!\n";
View
22 src/morph/Object.php
@@ -216,6 +216,17 @@ public function loadById($id)
{
return Storage::instance()->fetchById($this, $id);
}
+
+ /**
+ * Attempts to load the current object with data from the document id specified
+ *
+ * @param mixed $id
+ * @return Morph_Object
+ */
+ public function loadByObjectId($id)
+ {
+ return Storage::instance()->fetchByObjectId($this, $id);
+ }
/**
* Fetch multiple objects by their ids
@@ -227,6 +238,17 @@ public function findByIds(array $ids)
{
return Storage::instance()->fetchByIds($this, $ids);
}
+
+ /**
+ * Fetch multiple objects by their ids
+ *
+ * @param array $ids
+ * @return Morph_Iterator
+ */
+ public function findByObjectIds(array $ids)
+ {
+ return Storage::instance()->fetchByObjectIds($this, $ids);
+ }
/**
* Find objects by query
View
46 src/morph/Storage.php
@@ -94,6 +94,10 @@ public function getDatabase()
/**
* Retrieves the contents of the specified $id
* and assigns them into $object
+ *
+ * This shoudld be used if the _id is not necessarily a ObjectId()
+ * (or the php version, MongoId() ). You can always just wrap the id string
+ * in new \MongoId() as well
*
* @param Morph\\Object $object
* @param mixed $id
@@ -105,6 +109,25 @@ public function fetchById(Object $object, $id)
$data = $this->db->selectCollection($object->collection())->findOne($query);
return $this->setData($object, $data);
}
+
+ /**
+ * Retrieves the contents of the specified $id
+ * and assigns them into $object
+ * This function is meant for if the _id is a ObjectId (the default)
+ *
+ * @param Morph\\Object $object
+ * @param mixed $id
+ * @return Morph\\Object
+ */
+ public function fetchByObjectId(Object $object, $id)
+ {
+ if (is_string($id)) {
+ $id = new \MongoId($id);
+ }
+ $query = array('_id' => $id);
+ $data = $this->db->selectCollection($object->collection())->findOne($query);
+ return $this->setData($object, $data);
+ }
/**
* Returns all objects with an _id in $ids
@@ -119,6 +142,29 @@ public function fetchByIds(Object $object, array $ids)
$query->property('_id')->in($ids);
return $this->findByQuery($object, $query);
}
+
+ /**
+ * Returns all objects with an _id in $ids
+ *
+ * @param Morph\\Object $object
+ * @param array $Ids
+ * @return Morph\\Iterator
+ */
+ public function fetchObjectByIds(Object $object, array $ids)
+ {
+ $query = new Query();
+ $wrappedIds = array();
+ foreach ($ids as $id) {
+ if (is_string($id)) {
+ $wrappedIds[] = new \MongoId($id);
+ } else {
+ $wrappedIds[] = $id;
+ }
+ }
+
+ $query->property('_id')->in($ids);
+ return $this->findByQuery($object, $query);
+ }
/**
* Retrieves the contents of the specified $dbRef
Please sign in to comment.
Something went wrong with that request. Please try again.