2013-09-26 3 views

답변

2

당신이 을 던져하려면 적절한 예외 쿼리는 어떤 결과가 없습니다 저장소 (/ 내/내에서). 그런 다음 Doctrine\ORM\NoResultException을 사용해야합니다.

그런데 공유 한 코드 스 니펫을 저장소에서 사용해서는 안됩니다.

+0

docs에서 다음 코드 예제가 있습니다. $ query = $ em-> createQuery ('SELECT ...') -> setMaxResults (1); 시도 { $ product = $ query-> getSingleResult(); } catch (\ Doctrine \ Orm \ NoResultException $ e) { $ product = null; } – Acyra

1

try { 
      $em = $this->getDoctrine()->getManager(); 
      $entity = $em->getRepository('product')->find($id); 

      if (!$entity) { 
       $this->get('session')->setFlash('warning', 'Unable to find Product.'); 
      } 

      $em->remove($entity); 
      $em->flush(); 
      $this->get('session')->setFlash('success', 'Product Detail has been deleted.'); 
      return $this->redirect($this->generateUrl('admin_products')); 
     } catch (\Doctrine\DBAL\DBALException $e) { 
      $this->get('session')->setFlash(
        'warning', 'This Product cannot be deleted!' 
      ); 
      return $this->redirect($this->getRequest()->headers->get('referer')); 
     } 
    } 

추천하고 주어진 코드 아래에 나뭇 가지 템플릿 사용에 대한 오류 처리를 사용 세션 오류에 대한 또 다른 방법.

{% if app.session.hasFlash('success') %} 
    <div class="alert alert-success"> 
    {{ app.session.flash('success') }} 
     </div> 

{% endif %} 
    {% if app.session.hasFlash('warning') %} 
     <div class="alert alert-error"> 
    {{ app.session.flash('warning') }} 
      </div> 

{% endif %} 
관련 문제