Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/Parse/ParseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

@acinader acinader Oct 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false here

}
try {
$query = new ParseQuery($this->className);
$query->get($this->objectId, $useMasterKey);
return true;
} catch (Exception $e) {
if ($e->getMessage() === 'Object not found.') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this test the error code or number instead of the message string?

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);
}
}