What is a decent way to handle PDO errors when using a catch try block?
I currently have something like this:
BlogModel.php
class BlogModel extends Model { public function save($id, $value) { $stmt = $this->getDb()->prepare('UPDATE setting SET name = :name WHERE id = :id'); $stmt->bindParam(':id', $id); $stmt->bindParam(':name', $values); return ($stmt->execute() !== false) ? $id : false; } }
So, in the controller BlogController.php, I would do something like this:
<?php class Blog extends Controller { public function comments() { $data = array(); $model = new BlogModel; if ($model->save(2,'test')) { $data['result']['message'] = 'Settings saved'; $data['result']['status'] = 'success'; } else { $data['result']['message'] = 'Could not save the settings'; $data['result']['status'] = 'error'; } $view = new View("view.php", $data) $view->render(); } } ?>
This is how I handle the PDO error using if conditions. What is a decent way to translate this into a catch catch block? I don't want to encode variables ( $data['result']['message'] $data['result']['status'] ) all the time.
Is there any way to add a โthrow exceptionโ to the catch block?
If the controller has many lock attempts, it will look randomly .. right?
source share