2012-04-03 11 views
5

Yani atılan kılmasıdır, ben: {% render "EcsCrmBundle:Module:checkClock" %}Symfony2 dal, istisna benim taban şablonunda

Sonra

Ben ModuleController.php yarattı ...

<?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, 
     )); 
    } 
} 

şey de varsa sorun vardır Ben veritabanından hayır 'sonucu' olduğunu gerçeği ile ilgili bir şey var biliyorum

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 » 

... ama bu değil: belirli bir kullanıcı henüz için belirli bir gün için veritabanı .. i almaya devam ne Ben if (empty($entities)) { yaparak başardım ??

$entities = $query->getSingleResult(); 

Doktrin'e bakarsanız \ ORM \ AbstractQuery bunu göreceksiniz

$entity = $query->getOneOrNullResult(); 

olarak: Ben Değiştir hayır bunu düzeltmek için ipucu ... takdir herhangi bir yardım ...

cevap

19

var getSingleResult bir ve bir sonuç bekliyor. 0 bir istisna olacak.

Kodunuza biraz daha yakından baktım ve aslında bir dizi varlık beklediğinize benziyor. hangi durumda kullanılır:

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

Nice! Böyle bir konuyla uğraşıyordum ve sen benim için çözdün .. OP için +1 ve Cerad için +1! – Justin

+0

mükemmel çalıştı .. Cevabın seçiminde gecikme için üzgünüm .. – Johnny

İlgili konular