07/10/2014 - DOCTRINE, SYMFONY
Varsayalım ki, bir objeyi veritabanına yazarken oluşabilecek olan tüm muhtemel hataları try ... catch
bloğu içinde yakalamak istiyoruz.
namespace Football\FrontendBundle\Controller;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\ORMException;
use PDOException;
use Exception;
use Football\FrontendBundle\Entity\Country;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CountryController extends Controller
{
public function createAction()
{
$country = new Country();
$country->setCode('GB');
$country->setName('United Kingdom');
$em = $this->getDoctrine()->getEntityManager();
try {
$em->persist($country);
$em->flush();
$message = 'Done';
} catch (DBALException $e) {
$message = sprintf('DBALException [%i]: %s', $e->getCode(), $e->getMessage());
} catch (ORMException $e) {
$message = sprintf('ORMException [%i]: %s', $e->getCode(), $e->getMessage());
} catch (PDOException $e) {
$message = sprintf('PDOException [%i]: %s', $e->getCode(), $e->getMessage());
} catch (Exception $e) {
$message = sprintf('Exception [%i]: %s', $e->getCode(), $e->getMessage());
}
echo $message;
}
}