2012-04-03 3 views
5

を投げ、私が持っている:{% render "EcsCrmBundle:Module:checkClock" %}Symfony2の小枝がレンダリング、例外が私の基本テンプレートにそう

は、その後、私は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, 
     )); 
    } 
} 

何もに存在しない場合の問題は、あります特定のユーザーの特定の日のためのデータベースはまだ..私は得続ける:

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 » 

私はそれがデータベースからの「結果」ではないという事実とは何かを持っている知っている...しかし、それはありません何 私はif (empty($entities)) {を持っていますか?私はそれを修正するには見当もつかない...任意の助け感謝...

答えて

19

置き換えます

$entities = $query->getSingleResult(); 

$entity = $query->getOneOrNullResult(); 

を使用すると、教義\ ORM \ AbstractQueryで見れば、あなたはそれを見ますgetSingleResultは1つだけの結果を期待します。 0は例外を介して行われます。

私はあなたのコードを少し詳しく見ていて、実際にはエンティティの配列を期待しているようです。その場合は次を使用してください:

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

ニース!私はこのような問題に取り組んでいて、あなたは私のためにそれを解決しました.. OPのための+1とCeradの+1! – Justin

+0

は完璧に働きました。答えを選ぶのが遅れて申し訳ありません。 – Johnny

関連する問題