Skip to content

Commit

Permalink
feat: Add fetchWithInclude and fetchAllWithInclude
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis committed May 12, 2023
1 parent 77b06f4 commit 18785d4
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Parse/ParseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
*
Expand Down
54 changes: 54 additions & 0 deletions tests/Parse/ParseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1354,6 +1403,11 @@ public function testEmptyFetchAll()
$this->assertEmpty(ParseObject::fetchAll([]));
}

public function testEmptyFetchAllWithInclude()
{
$this->assertEmpty(ParseObject::fetchAllWithInclude([], []));
}

public function testFetchAllMixedClasses()
{
$this->expectException(
Expand Down

0 comments on commit 18785d4

Please sign in to comment.