How to use try-catch block for PDO

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?

+2
source share
3 answers

None of the answers here are so. But in fact, all three are combined with a real answer. You must install

 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

as said by Cerad .

From now on, every problem about something regarding the database is thrown except for the PDOException type. You just don't need to throw your own Exception , as ladar says, because it is useless. Just take the ladar code and convert it to

  ... $data = array(); $model = new BlogModel; try{ $model->save(2,'test'); $data['result']['message'] = 'Settings saved'; $data['result']['status'] = 'success'; }catch(PDOException $e){ $data['result']['message'] = 'Could not save the settings'; $data['result']['status'] = 'error'; } 

And DO NOT throw wow.

Then a very good way to debug PDO requests is using a Basic related script that you can find here again.

By combining these things, you get a flexible, clean and easy debugging way to catch all the errors that may occur.

+3
source

What about:

 class BlogModel extends Model { public function save($id, $value) { ... if (!$stmt->execute()) { throw new Exception($stmt->errorInfo()); } return $id; 

And then

  ... $data = array(); $model = new BlogModel; try{ $model->save(2,'test'); $data['result']['message'] = 'Settings saved'; $data['result']['status'] = 'success'; }catch(Exception $e){ $data['result']['message'] = 'Could not save the settings'; $data['result']['status'] = 'error'; } 
+3
source

Do you think PDO itself throws exceptions instead of errors?

 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

Now you can eliminate PDO error checking, which can significantly reduce code. You can add a try / catch block to catch exceptions that can be recovered, such as constraint violations.

For the rest, just use Try / Catch somewhere high up in your front controller to catch really exceptional exceptions.

The only difference in my approach is that in BlogModel you are just:

 $stmt->execute(); 

No verification or anything else. Just give PDO an exception if the insert failed. Then you will use try / catch in your controller as shown by the user

Or in my case, if I were sure that the insertion would never fail, I just use the common try / catch block in my front controller and do not waste time trying to handle every possible exception manually.

+3
source

All Articles