|
11 | 11 | use Parse\Internal\IncrementOperation;
|
12 | 12 | use Parse\Internal\RemoveOperation;
|
13 | 13 | use Parse\Internal\SetOperation;
|
| 14 | +use Zend\I18n\Exception\ParseException; |
14 | 15 |
|
15 | 16 | /**
|
16 | 17 | * ParseObject - Representation of an object stored on Parse.
|
@@ -511,6 +512,58 @@ public function fetch($useMasterKey = false)
|
511 | 512 | $this->_mergeAfterFetch($response);
|
512 | 513 | }
|
513 | 514 |
|
| 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 | + |
514 | 567 | /**
|
515 | 568 | * Merges data received from the server.
|
516 | 569 | *
|
@@ -589,6 +642,17 @@ private function mergeFromServer($data, $completeData = true)
|
589 | 642 | }
|
590 | 643 | }
|
591 | 644 |
|
| 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 | + |
592 | 656 | /**
|
593 | 657 | * Handle merging of special fields for the object.
|
594 | 658 | *
|
|
0 commit comments