symfony2 twig render, exception thrown

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

So in my base template, I have: {% render "EcsCrmBundle:Module:checkClock" %}

Then I created the 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,         ));     } } 

The problem is, if there is nothing in the database for the specific day for the specific user yet.. i keep getting:

I know it has something to do with the fact that is no 'result' from the database... but isn't that what i've accomplished by having the if (empty($entities)) { ?? I have no clue to fix it... any help appreciated...

回答1:

Replace:

$entities = $query->getSingleResult(); 

With

$entity = $query->getOneOrNullResult(); 

If you look in Doctrine\ORM\AbstractQuery you will see that getSingleResult expects one and only one results. 0 will through an exception.

I looked at your code a bit more closely and it looks like you actually expect an array of entities. in which case use:

$entities = $query->getResult(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!