From f82c73cd467344463f9a221828ff79bf83fe36cd Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 9 Oct 2019 12:04:24 -0500 Subject: [PATCH 1/3] Support object->exists() --- src/Parse/ParseObject.php | 22 ++++++++++++++++++++++ tests/Parse/ParseObjectTest.php | 15 +++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/Parse/ParseObject.php b/src/Parse/ParseObject.php index a88d5138..bb62c3a6 100644 --- a/src/Parse/ParseObject.php +++ b/src/Parse/ParseObject.php @@ -306,6 +306,28 @@ function ($object) use (&$result) { return $result; } + /** + * Returns true if this object exists on the Server + * + * @return bool + */ + public function exists($useMasterKey = false) + { + if (!$this->objectId) { + return; + } + try { + $query = new ParseQuery($this->className); + $query->get($this->objectId, $useMasterKey); + return true; + } catch (Exception $e) { + if ($e->getMessage() === 'Object not found.') { + 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); + } } From f46aec153713859e2e14226a1988c654b49238e7 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 9 Oct 2019 12:15:57 -0500 Subject: [PATCH 2/3] matches js sdk --- src/Parse/ParseObject.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Parse/ParseObject.php b/src/Parse/ParseObject.php index bb62c3a6..857bc73f 100644 --- a/src/Parse/ParseObject.php +++ b/src/Parse/ParseObject.php @@ -314,14 +314,14 @@ function ($object) use (&$result) { public function exists($useMasterKey = false) { if (!$this->objectId) { - return; + return false; } try { $query = new ParseQuery($this->className); $query->get($this->objectId, $useMasterKey); return true; } catch (Exception $e) { - if ($e->getMessage() === 'Object not found.') { + if ($e->getCode() === 101) { return false; } throw $e; From 120d6e49fb9fbcf8341cc75c8958b8abc0e72888 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 9 Oct 2019 12:23:13 -0500 Subject: [PATCH 3/3] Improve documentation --- src/Parse/ParseObject.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Parse/ParseObject.php b/src/Parse/ParseObject.php index 857bc73f..5c82fd31 100644 --- a/src/Parse/ParseObject.php +++ b/src/Parse/ParseObject.php @@ -309,6 +309,8 @@ function ($object) use (&$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)