Issue
There is no record with ID 0 on purpos. And I'm doing
$id = 0;
try {
$object = $this->MyModel->get($id);
} catch(Exception $e){
//Nothing
}
And I still get the exception thrown "Record not found in table".
How can I ignore, that there is no record with the given ID with get($id)
and avoid the exception?
Solution
$this->MyModel->find('all', ['conditions' => ['id' => $id]])->first();
seems to be the shortest code without getting an error on non-existence of an element.
The other problem was, that I used Exception
instead of the correct \Exception
, that's why the error was thrown despite the try-catch-block.
Answered By - Martin Cup
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.