gpt4 book ai didi

symfony2 Twig 渲染,抛出异常

转载 作者:行者123 更新时间:2023-12-04 16:27:47 33 4
gpt4 key购买 nike

所以在我的基本模板中,我有:{% 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,
));
}
}

问题是,如果特定用户在特定日期的数据库中没有任何内容......我不断收到:
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)) { 所取得的成就。 ??我不知道解决它...任何帮助表示赞赏...

最佳答案

代替:

$entities = $query->getSingleResult();


$entity = $query->getOneOrNullResult();

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

我更仔细地查看了您的代码,看起来您实际上期望的是一组实体。在这种情况下使用:
$entities = $query->getResult();

关于symfony2 Twig 渲染,抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985785/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com