Skip to content

Commit 22efa1c

Browse files
committed
Merge pull request #91 from ParsePlatform/gFosco.fetch-all
Adding ParseObject::fetchAll
2 parents 6b61d66 + 8ee59e6 commit 22efa1c

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Parse/ParseObject.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,58 @@ public function fetch($useMasterKey = false)
511511
$this->_mergeAfterFetch($response);
512512
}
513513

514+
/**
515+
* Fetch an array of Parse objects from the server
516+
*
517+
* @param array $objects The ParseObjects to fetch
518+
* @param boolean $useMasterKey Whether to override ACLs
519+
*
520+
* @return array
521+
*/
522+
public static function fetchAll(array $objects, $useMasterKey = false) {
523+
$objectIds = static::toObjectIdArray($objects);
524+
if (!count($objectIds)) return $objects;
525+
$className = $objects[0]->getClassName();
526+
$query = new ParseQuery($className);
527+
$query->containedIn("objectId", $objectIds);
528+
$query->limit(count($objectIds));
529+
$results = $query->find($useMasterKey);
530+
return static::updateWithFetchedResults($objects, $results);
531+
}
532+
533+
private static function toObjectIdArray(array $objects) {
534+
$objectIds = [];
535+
$count = count($objects);
536+
if (!$count) return $objectIds;
537+
$className = $objects[0]->getClassName();
538+
for ($i = 0; $i < $count; $i++) {
539+
$obj = $objects[$i];
540+
if ($obj->getClassName() !== $className) {
541+
throw new ParseException("All objects should be of the same class.");
542+
} else if (!$obj->getObjectId()) {
543+
throw new ParseException("All objects must have an ID.");
544+
}
545+
array_push($objectIds, $obj->getObjectId());
546+
}
547+
return $objectIds;
548+
}
549+
550+
private static function updateWithFetchedResults(array $objects, array $fetched) {
551+
$fetchedObjectsById = [];
552+
foreach ($fetched as $object) {
553+
$fetchedObjectsById[$object->getObjectId()] = $object;
554+
}
555+
$count = count($objects);
556+
for ($i = 0; $i < $count; $i++) {
557+
$obj = $objects[$i];
558+
if (!isset($fetchedObjectsById[$obj->getObjectId()])) {
559+
throw new ParseException("All objects must exist on the server.");
560+
}
561+
$obj->mergeFromObject($fetchedObjectsById[$obj->getObjectId()]);
562+
}
563+
return $objects;
564+
}
565+
514566
/**
515567
* Merges data received from the server.
516568
*
@@ -589,6 +641,17 @@ private function mergeFromServer($data, $completeData = true)
589641
}
590642
}
591643

644+
private function mergeFromObject($other) {
645+
if (!$other) return;
646+
$this->objectId = $other->getObjectId();
647+
$this->createdAt = $other->getCreatedAt();
648+
$this->updatedAt = $other->getUpdatedAt();
649+
$this->serverData = $other->serverData;
650+
$this->operationSet = [];
651+
$this->hasBeenFetched = true;
652+
$this->rebuildEstimatedData();
653+
}
654+
592655
/**
593656
* Handle merging of special fields for the object.
594657
*

tests/ParseObjectTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,4 +935,23 @@ public function testBatchSaveExceptions()
935935
$this->assertContains("invalid field name", $errors[1]['error']);
936936
}
937937
}
938+
939+
public function testFetchAll()
940+
{
941+
$obj1 = ParseObject::create("TestObject");
942+
$obj2 = ParseObject::create("TestObject");
943+
$obj3 = ParseObject::create("TestObject");
944+
$obj1->set("foo", "bar");
945+
$obj2->set("foo", "bar");
946+
$obj3->set("foo", "bar");
947+
ParseObject::saveAll([$obj1, $obj2, $obj3]);
948+
$newObj1 = ParseObject::create("TestObject", $obj1->getObjectId());
949+
$newObj2 = ParseObject::create("TestObject", $obj2->getObjectId());
950+
$newObj3 = ParseObject::create("TestObject", $obj3->getObjectId());
951+
$results = ParseObject::fetchAll([$newObj1, $newObj2, $newObj3]);
952+
$this->assertEquals(3, count($results));
953+
$this->assertEquals("bar", $results[0]->get("foo"));
954+
$this->assertEquals("bar", $results[1]->get("foo"));
955+
$this->assertEquals("bar", $results[2]->get("foo"));
956+
}
938957
}

0 commit comments

Comments
 (0)