Skip to content

Commit

Permalink
Support object->exists() (#453)
Browse files Browse the repository at this point in the history
* Support object->exists()

* matches js sdk

* Improve documentation
  • Loading branch information
dplewis authored Oct 9, 2019
1 parent 8b7087a commit eada867
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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);
}
}

0 comments on commit eada867

Please sign in to comment.