Skip to content

Adding ParseObject::fetchAll #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Parse/ParseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,58 @@ public function fetch($useMasterKey = false)
$this->_mergeAfterFetch($response);
}

/**
* Fetch an array of Parse objects from the server
*
* @param array $objects The ParseObjects to fetch
* @param boolean $useMasterKey Whether to override ACLs
*
* @return array
*/
public static function fetchAll(array $objects, $useMasterKey = false) {
$objectIds = static::toObjectIdArray($objects);
if (!count($objectIds)) return $objects;
$className = $objects[0]->getClassName();
$query = new ParseQuery($className);
$query->containedIn("objectId", $objectIds);
$query->limit(count($objectIds));
$results = $query->find($useMasterKey);
return static::updateWithFetchedResults($objects, $results);
}

private static function toObjectIdArray(array $objects) {
$objectIds = [];
$count = count($objects);
if (!$count) return $objectIds;
$className = $objects[0]->getClassName();
for ($i = 0; $i < $count; $i++) {
$obj = $objects[$i];
if ($obj->getClassName() !== $className) {
throw new ParseException("All objects should be of the same class.");
} else if (!$obj->getObjectId()) {
throw new ParseException("All objects must have an ID.");
}
array_push($objectIds, $obj->getObjectId());
}
return $objectIds;
}

private static function updateWithFetchedResults(array $objects, array $fetched) {
$fetchedObjectsById = [];
foreach ($fetched as $object) {
$fetchedObjectsById[$object->getObjectId()] = $object;
}
$count = count($objects);
for ($i = 0; $i < $count; $i++) {
$obj = $objects[$i];
if (!isset($fetchedObjectsById[$obj->getObjectId()])) {
throw new ParseException("All objects must exist on the server.");
}
$obj->mergeFromObject($fetchedObjectsById[$obj->getObjectId()]);
}
return $objects;
}

/**
* Merges data received from the server.
*
Expand Down Expand Up @@ -589,6 +641,17 @@ private function mergeFromServer($data, $completeData = true)
}
}

private function mergeFromObject($other) {
if (!$other) return;
$this->objectId = $other->getObjectId();
$this->createdAt = $other->getCreatedAt();
$this->updatedAt = $other->getUpdatedAt();
$this->serverData = $other->serverData;
$this->operationSet = [];
$this->hasBeenFetched = true;
$this->rebuildEstimatedData();
}

/**
* Handle merging of special fields for the object.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/ParseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,23 @@ public function testBatchSaveExceptions()
$this->assertContains("invalid field name", $errors[1]['error']);
}
}

public function testFetchAll()
{
$obj1 = ParseObject::create("TestObject");
$obj2 = ParseObject::create("TestObject");
$obj3 = ParseObject::create("TestObject");
$obj1->set("foo", "bar");
$obj2->set("foo", "bar");
$obj3->set("foo", "bar");
ParseObject::saveAll([$obj1, $obj2, $obj3]);
$newObj1 = ParseObject::create("TestObject", $obj1->getObjectId());
$newObj2 = ParseObject::create("TestObject", $obj2->getObjectId());
$newObj3 = ParseObject::create("TestObject", $obj3->getObjectId());
$results = ParseObject::fetchAll([$newObj1, $newObj2, $newObj3]);
$this->assertEquals(3, count($results));
$this->assertEquals("bar", $results[0]->get("foo"));
$this->assertEquals("bar", $results[1]->get("foo"));
$this->assertEquals("bar", $results[2]->get("foo"));
}
}