Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support object->exists() #453

Merged
merged 3 commits into from
Oct 9, 2019
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
24 changes: 24 additions & 0 deletions src/Parse/ParseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,30 @@ function ($object) use (&$result) {
return $result;
}

/**
* Returns true if this object exists on the Server
*
* @param bool $useMasterKey Whether to use the Master Key.
*
* @return bool
*/
public function exists($useMasterKey = false)
{
if (!$this->objectId) {
return false;
}
try {
$query = new ParseQuery($this->className);
$query->get($this->objectId, $useMasterKey);
return true;
} catch (Exception $e) {
if ($e->getCode() === 101) {
return false;
}
throw $e;
}
}

/**
* Validate and set a value for an object key.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Parse/ParseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1775,4 +1775,19 @@ public function testUnrecognizedOp()
];
ParseObject::decode($encoded);
}

/**
* Tests if object exists
*
* @group object-exists
*/
public function testObjectExists()
{
$obj = new ParseObject('TestClass');
$this->assertEquals($obj->exists(), false);
$obj->save();
$this->assertEquals($obj->exists(), true);
$obj->destroy();
$this->assertEquals($obj->exists(), false);
}
}