所以在我的基本模板中,我有: {% 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,
));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果特定用户的特定日期数据库中没有任何内容,我仍然会:
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 »
Run Code Online (Sandbox Code Playgroud)
我知道它与数据库中没有"结果"的事实有关...但不是因为我已经完成了什么if (empty($entities)) {?我无法解决这个问题...任何帮助表示赞赏......
Cer*_*rad 19
更换:
$entities = $query->getSingleResult();
Run Code Online (Sandbox Code Playgroud)
同
$entity = $query->getOneOrNullResult();
Run Code Online (Sandbox Code Playgroud)
如果您查看Doctrine\ORM\AbstractQuery,您将看到getSingleResult只需要一个结果.0将通过例外.
我仔细查看了你的代码,看起来你实际上期望一组实体.在这种情况下使用:
$entities = $query->getResult();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5793 次 |
| 最近记录: |