Skip to content

Commit 0570f15

Browse files
authored
feat: Add query methods fetchWithInclude and fetchAllWithInclude (#512)
1 parent 45d8ad7 commit 0570f15

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

src/Parse/ParseObject.php

+52
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,33 @@ public function fetch($useMasterKey = false)
570570
return $this;
571571
}
572572

573+
/**
574+
* Fetch an array of Parse objects from the server.
575+
*
576+
* @param array $objects The ParseObjects to fetch
577+
* @param array $includeKeys The nested ParseObjects to fetch
578+
* @param bool $useMasterKey Whether to override ACLs
579+
*
580+
* @return ParseObject Returns self, so you can chain this call.
581+
*/
582+
public function fetchWithInclude(array $includeKeys, $useMasterKey = false)
583+
{
584+
$sessionToken = null;
585+
if (ParseUser::getCurrentUser()) {
586+
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
587+
}
588+
$response = ParseClient::_request(
589+
'GET',
590+
'classes/'.$this->className.'/'.$this->objectId.'?include='.implode(',', $includeKeys),
591+
$sessionToken,
592+
null,
593+
$useMasterKey
594+
);
595+
$this->_mergeAfterFetch($response);
596+
597+
return $this;
598+
}
599+
573600
/**
574601
* Fetch an array of Parse objects from the server.
575602
*
@@ -593,6 +620,31 @@ public static function fetchAll(array $objects, $useMasterKey = false)
593620
return static::updateWithFetchedResults($objects, $results);
594621
}
595622

623+
/**
624+
* Fetch an array of Parse Objects from the server with nested Parse Objects.
625+
*
626+
* @param array $objects The ParseObjects to fetch
627+
* @param mixed $includeKeys The nested ParseObjects to fetch
628+
* @param bool $useMasterKey Whether to override ACLs
629+
*
630+
* @return array
631+
*/
632+
public static function fetchAllWithInclude(array $objects, $includeKeys, $useMasterKey = false)
633+
{
634+
$objectIds = static::toObjectIdArray($objects);
635+
if (!count($objectIds)) {
636+
return $objects;
637+
}
638+
$className = $objects[0]->getClassName();
639+
$query = new ParseQuery($className);
640+
$query->containedIn('objectId', $objectIds);
641+
$query->limit(count($objectIds));
642+
$query->includeKey($includeKeys);
643+
$results = $query->find($useMasterKey);
644+
645+
return static::updateWithFetchedResults($objects, $results);
646+
}
647+
596648
/**
597649
* Creates an array of object ids from a given array of ParseObjects
598650
*

tests/Parse/ParseObjectTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,55 @@ public function testFetchAll()
10951095
$this->assertEquals('bar', $results[2]->get('foo'));
10961096
}
10971097

1098+
/**
1099+
* @group test-fetch-all-include
1100+
*/
1101+
public function testFetchAllWithInclude()
1102+
{
1103+
$child = ParseObject::create('TestObject');
1104+
$child->set('name', 'parse');
1105+
$child->save();
1106+
$obj1 = ParseObject::create('TestObject');
1107+
$obj2 = ParseObject::create('TestObject');
1108+
$obj3 = ParseObject::create('TestObject');
1109+
$obj1->set('foo', 'bar');
1110+
$obj2->set('foo', 'bar');
1111+
$obj3->set('foo', 'bar');
1112+
$obj1->set('child', $child);
1113+
$obj2->set('child', $child);
1114+
$obj3->set('child', $child);
1115+
ParseObject::saveAll([$obj1, $obj2, $obj3]);
1116+
$newObj1 = ParseObject::create('TestObject', $obj1->getObjectId());
1117+
$newObj2 = ParseObject::create('TestObject', $obj2->getObjectId());
1118+
$newObj3 = ParseObject::create('TestObject', $obj3->getObjectId());
1119+
$results = ParseObject::fetchAllWithInclude([$newObj1, $newObj2, $newObj3], ['child']);
1120+
$this->assertEquals(3, count($results));
1121+
$this->assertEquals('bar', $results[0]->get('foo'));
1122+
$this->assertEquals('bar', $results[1]->get('foo'));
1123+
$this->assertEquals('bar', $results[2]->get('foo'));
1124+
$this->assertEquals('parse', $results[0]->get('child')->get('name'));
1125+
$this->assertEquals('parse', $results[1]->get('child')->get('name'));
1126+
$this->assertEquals('parse', $results[2]->get('child')->get('name'));
1127+
}
1128+
1129+
/**
1130+
* @group test-fetch-include
1131+
*/
1132+
public function testFetchWithInclude()
1133+
{
1134+
$child = ParseObject::create('TestObject');
1135+
$child->set('name', 'parse');
1136+
$child->save();
1137+
$obj1 = ParseObject::create('TestObject');
1138+
$obj1->set('foo', 'bar');
1139+
$obj1->set('child', $child);
1140+
$obj1->save();
1141+
$newObj1 = ParseObject::create('TestObject', $obj1->getObjectId());
1142+
$newObj1->fetchWithInclude(['child']);
1143+
$this->assertEquals('bar', $newObj1->get('foo'));
1144+
$this->assertEquals('parse', $newObj1->get('child')->get('name'));
1145+
}
1146+
10981147
public function testNoRegisteredSubclasses()
10991148
{
11001149
$this->expectException(
@@ -1354,6 +1403,11 @@ public function testEmptyFetchAll()
13541403
$this->assertEmpty(ParseObject::fetchAll([]));
13551404
}
13561405

1406+
public function testEmptyFetchAllWithInclude()
1407+
{
1408+
$this->assertEmpty(ParseObject::fetchAllWithInclude([], []));
1409+
}
1410+
13571411
public function testFetchAllMixedClasses()
13581412
{
13591413
$this->expectException(

tests/Parse/ParseUserTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,41 @@ public function testUserAssociations()
525525
);
526526
}
527527

528+
/**
529+
* @group test-fetch-include
530+
*/
531+
public function testUserFetchWithInclude()
532+
{
533+
$child = ParseObject::create('TestObject');
534+
$child->set('name', 'parsephp');
535+
$child->save();
536+
537+
$user = new ParseUser();
538+
$user->setUsername('asdf');
539+
$user->setPassword('zxcv');
540+
$user->set('child', $child);
541+
$user->signUp();
542+
543+
$object = ParseObject::create('TestObject');
544+
$object->set('user', $user);
545+
$object->save();
546+
547+
$query = new ParseQuery('TestObject');
548+
$objectAgain = $query->get($object->getObjectId());
549+
$userAgain = $objectAgain->get('user');
550+
$userAgain->fetchWithInclude(['child']);
551+
552+
$this->assertEquals($userAgain->getObjectId(), $user->getObjectId());
553+
$this->assertEquals(
554+
$userAgain->get('child')->getObjectId(),
555+
$child->getObjectId()
556+
);
557+
$this->assertEquals(
558+
$userAgain->get('child')->get('name'),
559+
$child->get('name')
560+
);
561+
}
562+
528563
public function testUserQueries()
529564
{
530565
Helper::clearClass(ParseUser::$parseClassName);

0 commit comments

Comments
 (0)