diff --git a/src/Parse/ParseObject.php b/src/Parse/ParseObject.php index a88d5138..5c82fd31 100644 --- a/src/Parse/ParseObject.php +++ b/src/Parse/ParseObject.php @@ -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. * diff --git a/tests/Parse/ParseObjectTest.php b/tests/Parse/ParseObjectTest.php index becddca6..d0d4e5aa 100644 --- a/tests/Parse/ParseObjectTest.php +++ b/tests/Parse/ParseObjectTest.php @@ -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); + } }