2012-04-03 22 views
5

Quindi, nel mio modello di base, ho: {% render "EcsCrmBundle:Module:checkClock" %}Symfony2 ramoscello render, eccezione generata

Poi ho creato il ModuleController.php ...

<?php 

namespace Ecs\CrmBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Ecs\CrmBundle\Entity\TimeClock; 

class ModuleController extends Controller 
{ 
    public function checkClockAction() { 
     $em = $this->getDoctrine()->getEntityManager(); 
     $user = $this->get('security.context')->getToken()->getUser(); 
     $today = time(); 
     $start = date('Y-m-d 00:00:00'); 
     $entities = $em->getRepository('EcsCrmBundle:TimeClock'); 
     $query = $entities->createQueryBuilder('tc') 
       ->select('tc.in1, tc.out1, tc.in2, tc.out2, tc.in3, tc.out3') 
       ->where('tc.noteBy = :user') 
       ->andWhere('tc.daydate >= :start') 
       ->setParameter('user', $user->getid()) 
       ->setParameter('start', $start) 
       ->setMaxResults('1') 
       ->getQuery(); 
     $entities = $query->getSingleResult(); 
     if (empty($entities)) { 
      $ents = "clocked_out"; 
      $this->get('session')->set('clockedin', 'clocked_out'); 
     } else { 
      for ($i=1; $i <= 3; $i++) { 
       if ($entities["in$i"] != NULL) { 
        $ents = "clocked_in"; 
        if ($i == 1) { 
         $this->get('session')->set('nextclock', "out$i"); 
        } else { 
         $x = $i+1; 
         $this->get('session')->set('nextClock', "out$x"); 
        } 
        if ($entities["out$i"] != NULL) { 
         $ents = "clocked_out"; 
         $x = $i+1; 
         $this->get('session')->set('nextclock', "in$x"); 
        } 
        if ($entities["out3"] != NULL) { 
         $ents = "day_done"; 
        } 
       } 
      } 
     } 
     return $this->render('EcsCrmBundle:Module:topclock.html.twig', array(
      'cstat' => $ents, 
     )); 
    } 
} 

Il problema è che se non c'è nulla nel database per il giorno specifico per l'utente specifico ancora .. Continuo a ricevere:

An exception has been thrown during the rendering of a template ("No result was found for query although at least one row was expected.") in ::base.html.twig at line 161. 
500 Internal Server Error - Twig_Error_Runtime 
1 linked Exception: NoResultException » 

so che ha qualcosa a che fare con il fatto che non è 'risultato' dal database ... ma non è che che cosa ho realizzato avendo lo if (empty($entities)) { ?? Non ho idea di risolvere il problema ... qualsiasi aiuto apprezzato ...

risposta

19

Sostituire:

$entities = $query->getSingleResult(); 

Con

$entity = $query->getOneOrNullResult(); 

Se si guarda in Dottrina \ ORM \ AbstractQuery vedrete che getSingleResult si aspetta un solo risultato. 0 effettuerà un'eccezione.

Ho esaminato il tuo codice un po 'più da vicino e sembra che tu ti aspetti davvero una serie di entità. nel qual caso utilizzare:

$entities = $query->getResult(); 
+1

Bello! Avevo a che fare con un problema molto simile e tu l'hai risolto per me .. +1 per l'OP e +1 per Cerad! – Justin

+0

ha funzionato perfettamente .. scusa per il ritardo nella selezione della risposta .. – Johnny

Problemi correlati