symfony2细枝渲染,引发异常

symfony2细枝渲染,引发异常,symfony,twig,Symfony,Twig,因此,在我的基本模板中,我有:{%render“EcsCrmBundle:Module:checkClock”%} 然后我创建了ModuleController.php <?php namespace Ecs\CrmBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Ecs\CrmBundle\Entity\TimeClock; class ModuleController

因此,在我的基本模板中,我有:
{%render“EcsCrmBundle:Module:checkClock”%}

然后我创建了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,
        ));
    }
}
我知道这与数据库没有“结果”有关。。。但这不是我通过让
if(空($entities)){
完成的吗?我没有任何线索来修复它…感谢任何帮助…

替换:

$entities = $query->getSingleResult();

如果您查看Doctrine\ORM\AbstractQuery,您将看到getSingleResult需要一个且只有一个结果。0将通过一个异常

我更仔细地查看了您的代码,看起来您实际上希望看到一个实体数组。在这种情况下,请使用:

$entities = $query->getResult();

很好!我正在处理一个类似的问题,你帮我解决了..OP+1,Cerad+1!
$entity = $query->getOneOrNullResult();
$entities = $query->getResult();