Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 get error code for flush function

I am using Symfony and creating a soft with 3 tiers (client, apache, mysql). So I don't know how to get the statut when the symfony application persists and flushs something?

When I add something in database I display an alert like "Add done!" but if my database is down I will display "Add done" despite the fail...

So how can I get the statut of these functions (flush/persist)? How can I change my alert switch the statut?

Best regards,

like image 760
Vender Aeloth Avatar asked Apr 24 '14 13:04

Vender Aeloth


1 Answers

Use a try & catch block:

try {
     $article = new Article(); //Example entity
     $em = $this->getDoctrine()->getEntityManager();
     $em->persist($article);
     $em->flush();  
     $this->get('session')->setFlash('flash_key',"Add done!");

 } catch (Exception $e) {
     $this->get('session')->setFlash('flash_key',"Add not done: " . $e->getMessage());
 }

In case you get errors try using "\Doctrine\ORM\ORMException $e", \Doctrine\DBAL\DBALException $e" or "\Exception $e" inside catch()

like image 143
Alfonso Jiménez Avatar answered Oct 14 '22 09:10

Alfonso Jiménez