diff --git a/src/Parse/ParseObject.php b/src/Parse/ParseObject.php index 6345bba9..26ce7e11 100644 --- a/src/Parse/ParseObject.php +++ b/src/Parse/ParseObject.php @@ -570,6 +570,33 @@ public function fetch($useMasterKey = false) return $this; } + /** + * Fetch an array of Parse objects from the server. + * + * @param array $objects The ParseObjects to fetch + * @param array $includeKeys The nested ParseObjects to fetch + * @param bool $useMasterKey Whether to override ACLs + * + * @return ParseObject Returns self, so you can chain this call. + */ + public function fetchWithInclude(array $includeKeys, $useMasterKey = false) + { + $sessionToken = null; + if (ParseUser::getCurrentUser()) { + $sessionToken = ParseUser::getCurrentUser()->getSessionToken(); + } + $response = ParseClient::_request( + 'GET', + 'classes/'.$this->className.'/'.$this->objectId.'?include='.implode(',', $includeKeys), + $sessionToken, + null, + $useMasterKey + ); + $this->_mergeAfterFetch($response); + + return $this; + } + /** * Fetch an array of Parse objects from the server. * @@ -593,6 +620,31 @@ public static function fetchAll(array $objects, $useMasterKey = false) return static::updateWithFetchedResults($objects, $results); } + /** + * Fetch an array of Parse Objects from the server with nested Parse Objects. + * + * @param array $objects The ParseObjects to fetch + * @param mixed $includeKeys The nested ParseObjects to fetch + * @param bool $useMasterKey Whether to override ACLs + * + * @return array + */ + public static function fetchAllWithInclude(array $objects, $includeKeys, $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)); + $query->includeKey($includeKeys); + $results = $query->find($useMasterKey); + + return static::updateWithFetchedResults($objects, $results); + } + /** * Creates an array of object ids from a given array of ParseObjects * diff --git a/tests/Parse/ParseObjectTest.php b/tests/Parse/ParseObjectTest.php index d0d4e5aa..857bc36b 100644 --- a/tests/Parse/ParseObjectTest.php +++ b/tests/Parse/ParseObjectTest.php @@ -1095,6 +1095,55 @@ public function testFetchAll() $this->assertEquals('bar', $results[2]->get('foo')); } + /** + * @group test-fetch-all-include + */ + public function testFetchAllWithInclude() + { + $child = ParseObject::create('TestObject'); + $child->set('name', 'parse'); + $child->save(); + $obj1 = ParseObject::create('TestObject'); + $obj2 = ParseObject::create('TestObject'); + $obj3 = ParseObject::create('TestObject'); + $obj1->set('foo', 'bar'); + $obj2->set('foo', 'bar'); + $obj3->set('foo', 'bar'); + $obj1->set('child', $child); + $obj2->set('child', $child); + $obj3->set('child', $child); + 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::fetchAllWithInclude([$newObj1, $newObj2, $newObj3], ['child']); + $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')); + $this->assertEquals('parse', $results[0]->get('child')->get('name')); + $this->assertEquals('parse', $results[1]->get('child')->get('name')); + $this->assertEquals('parse', $results[2]->get('child')->get('name')); + } + + /** + * @group test-fetch-include + */ + public function testFetchWithInclude() + { + $child = ParseObject::create('TestObject'); + $child->set('name', 'parse'); + $child->save(); + $obj1 = ParseObject::create('TestObject'); + $obj1->set('foo', 'bar'); + $obj1->set('child', $child); + $obj1->save(); + $newObj1 = ParseObject::create('TestObject', $obj1->getObjectId()); + $newObj1->fetchWithInclude(['child']); + $this->assertEquals('bar', $newObj1->get('foo')); + $this->assertEquals('parse', $newObj1->get('child')->get('name')); + } + public function testNoRegisteredSubclasses() { $this->expectException( @@ -1354,6 +1403,11 @@ public function testEmptyFetchAll() $this->assertEmpty(ParseObject::fetchAll([])); } + public function testEmptyFetchAllWithInclude() + { + $this->assertEmpty(ParseObject::fetchAllWithInclude([], [])); + } + public function testFetchAllMixedClasses() { $this->expectException( diff --git a/tests/Parse/ParseUserTest.php b/tests/Parse/ParseUserTest.php index 52ee6fa8..1041ad3e 100644 --- a/tests/Parse/ParseUserTest.php +++ b/tests/Parse/ParseUserTest.php @@ -525,6 +525,41 @@ public function testUserAssociations() ); } + /** + * @group test-fetch-include + */ + public function testUserFetchWithInclude() + { + $child = ParseObject::create('TestObject'); + $child->set('name', 'parsephp'); + $child->save(); + + $user = new ParseUser(); + $user->setUsername('asdf'); + $user->setPassword('zxcv'); + $user->set('child', $child); + $user->signUp(); + + $object = ParseObject::create('TestObject'); + $object->set('user', $user); + $object->save(); + + $query = new ParseQuery('TestObject'); + $objectAgain = $query->get($object->getObjectId()); + $userAgain = $objectAgain->get('user'); + $userAgain->fetchWithInclude(['child']); + + $this->assertEquals($userAgain->getObjectId(), $user->getObjectId()); + $this->assertEquals( + $userAgain->get('child')->getObjectId(), + $child->getObjectId() + ); + $this->assertEquals( + $userAgain->get('child')->get('name'), + $child->get('name') + ); + } + public function testUserQueries() { Helper::clearClass(ParseUser::$parseClassName);