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 be loaded ok too, but before that, it is a problem, getRandomBrokenTrollType() deliberately does not exist, and then I see message in 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 it does not happen
How to check existing of a object's method?
if you're trying to catch any exception, you should use a backslash before "Exception".
E.g.:
try{
//do stuff here
}
catch(\Exception $e){
error_log($e->getMessage());
}
If you don't use a backslash, the exception won't be caught. This is due to how namespaces are used in PHP / Symfony.
the Exception thrown by Doctrine is called Doctrine\ORM\EntityNotFoundException
and you are catching EntityNotFoundException
.
Thats a difference, the namespace matters.
to debug this, catch Exception
instead and observe the type of the actual exception. then replace it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With