Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions tests/Parse/ParseUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down