Q:

catch integrity constraint error in symfony and doctrine

I have two tables which are: 
Category:
|id | cat_name  |
| 1 | nameone   |
| 2 | nametwo   |   
| 3 | namethree |

News:
| id | id_cat | title    |
| 1  | 1      | title1 |
| 2  | 2      | title2 |
| 3  | 3      | title3 |
| 4  | 3      | title4 |
| 5  | 3      | title5 |

i use doctrine:generate-module backend category Category and i have a list Category with executeEdit, Update etc. If i delete Category id 3 - namethree then I get the following error 

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or
  update a parent row: a foreign key constraint fails .

How can I make sure that if I delete Category 3 then all News for Category 3 are moved to Category 2 for example without an error? How can catch this?

A:

You have to configure your table schema for News to handle foreign key changes with ON DELETE: Either "set to null", or "set to default", or "set to specific value", or "delete". Not sure if you can do this variably, though, but if you think about it that'd be insane: You have referential integrity precisely because you want your relations to be strictly enforced. Randomly changing them if you delete a category row would go completely counter to that.
Much better to handle this correctly yourself by first UPDATEing the to-be-modified rows.
UPDATE News SET id_cat = 2 WHERE id_cat = 3;
DELETE FROM Category WHERE id = 3;

(I don't know the appropriate Symfony/Doctrine method to affect this.)
Your available options depend on your database. You can always say ON DELETE CASCADE to delete all dependent rows; other options may or may not be available to you.

