How to try to catch in symfony

Situation:

//trollCommand.php [...] foreach ($trolltypes as $type) { //$type=={"Frost","RandomBroken","Forest"} try { $output->writeln($type); $troll={"get".$type."TrollType"}(); $output->writeln("TEST 1"); $troll->__load(); $output->writeln("TEST 2"); } catch (EntityNotFoundException $e) { $output->writeln("WARNING: TROLL ENTITY DOES NOT EXIST."); continue; } $output->writeln("TROLLING"); do_something_with_troll($troll); } 

getFrostTrollType loads ok, getForestTrollType should also be loaded by the window, but before that it is a problem, getRandomBrokenTrollType () intentionally does not exist, and then I see a message in the console:

  Frost Test 1 Test 2 TROLLING RandomBroken Test 1 [Doctrine\ORM\EntityNotFoundException] Entity was not found. //[EXIT FROM SCRIPT] troll@troll-machine ~/trollSandbox/ $ _ 

it should be: WARNING: TROLL ENTITY DOES NOT EXIST. and then continue; but this does not happen

How to check existing object method?

+7
php symfony
source share
3 answers

The exception Doctrine\ORM\EntityNotFoundException by Doctrine is called Doctrine\ORM\EntityNotFoundException , and you catch an EntityNotFoundException .

This is the difference, the namespace matters.

to debug this, catch an Exception and look at the type of the actual exception. then replace it.

+4
source share

if you are trying to catch any exception, you must use the backslash before the β€œException”.

eg:.

 try{ //do stuff here } catch(\Exception $e){ error_log($e->getMessage()); } 

If you do not use a backslash, an exception will not be detected. This is due to the way namespaces are used in PHP / Symfony.

+15
source share

The type of exception is \ Doctrine \ ORM \ EntityNotFoundException Do not forget to run "\" Example -

  try { $entityManager = $this->getEntityManager(); $entityManager->remove($entity); $entityManager->flush(); // save to the database } catch (\Doctrine\ORM\EntityNotFoundException $ex) { echo "Exception Found - " . $ex->getMessage() . "<br/>"; } 
+1
source share

All Articles