Skip to content

Commit eada867

Browse files
authored
Support object->exists() (#453)
* Support object->exists() * matches js sdk * Improve documentation
1 parent 8b7087a commit eada867

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Parse/ParseObject.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,30 @@ function ($object) use (&$result) {
306306
return $result;
307307
}
308308

309+
/**
310+
* Returns true if this object exists on the Server
311+
*
312+
* @param bool $useMasterKey Whether to use the Master Key.
313+
*
314+
* @return bool
315+
*/
316+
public function exists($useMasterKey = false)
317+
{
318+
if (!$this->objectId) {
319+
return false;
320+
}
321+
try {
322+
$query = new ParseQuery($this->className);
323+
$query->get($this->objectId, $useMasterKey);
324+
return true;
325+
} catch (Exception $e) {
326+
if ($e->getCode() === 101) {
327+
return false;
328+
}
329+
throw $e;
330+
}
331+
}
332+
309333
/**
310334
* Validate and set a value for an object key.
311335
*

tests/Parse/ParseObjectTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,4 +1775,19 @@ public function testUnrecognizedOp()
17751775
];
17761776
ParseObject::decode($encoded);
17771777
}
1778+
1779+
/**
1780+
* Tests if object exists
1781+
*
1782+
* @group object-exists
1783+
*/
1784+
public function testObjectExists()
1785+
{
1786+
$obj = new ParseObject('TestClass');
1787+
$this->assertEquals($obj->exists(), false);
1788+
$obj->save();
1789+
$this->assertEquals($obj->exists(), true);
1790+
$obj->destroy();
1791+
$this->assertEquals($obj->exists(), false);
1792+
}
17781793
}

0 commit comments

Comments
 (0)