How to handle exception thrown "Integrity constraint violation: Can not delete or update parent row: a foreign key constraint fails"?

3

I use the class PDO for communication with banco de dados and I do not know a good way to capture this exceção .

I searched for a class that extends PDOException but did not find it.

I can make a query before deleting, this resolves, but I wanted to handle when the exception is thrown, or is that not the best way?

    
asked by anonymous 29.01.2015 / 19:53

1 answer

1

There are cases where the getCode() of the PDOException does not return the correct code.

In this case I use:

try {
   ....
} catch (PDOException $e) {
   if (isset($e->errorInfo[1]) && $e->errorInfo[1] == '1451') {
      print 'mensagem';
   }
}
    
30.01.2015 / 17:03