Skip to content

Commit 54f84c4

Browse files
author
Fosco Marotto
committed
Adding ParseObject::fetchAll
1 parent a192709 commit 54f84c4

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/Parse/ParseObject.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Parse\Internal\IncrementOperation;
1212
use Parse\Internal\RemoveOperation;
1313
use Parse\Internal\SetOperation;
14+
use Zend\I18n\Exception\ParseException;
1415

1516
/**
1617
* ParseObject - Representation of an object stored on Parse.
@@ -511,6 +512,58 @@ public function fetch($useMasterKey = false)
511512
$this->_mergeAfterFetch($response);
512513
}
513514

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

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

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)