2012-04-03 17 views
0

我创建了一个控制器,每个页面上运行,它的工作只是一个方面..Symfony2的会议需要2次刷新更新

在我base.html.twig文件我把:{% render "EcsCrmBundle:Module:checkClock" %}

所以,我有一个控制器所谓ModuleController与包含此代码的checkClockAction动作:

<?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->getOneOrNullResult(); 
     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"; 
        $this->get('session')->set('clockedin', '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"; 
         $this->get('session')->set('clockedin', 'clocked_out'); 
         $x = $i+1; 
         $this->get('session')->set('nextclock', "in$x"); 
        } 
        if ($entities["out3"] != NULL) { 
         $ents = "day_done"; 
         $this->get('session')->set('clockedin', 'day_done'); 
        } 
       } 
      } 
     } 
     return $this->render('EcsCrmBundle:Module:topclock.html.twig', array(
      'cstat' => $ents, 
     )); 
    } 
} 

这是工作很好,除了当我在登录/注销按钮,一按...

<a href="{{ path('timeclock_clockin') }}" class="clockin clocker">Clock In</a>

这是通过jQuery Ajax和提交:

$(".clocker").click(function() { 
     // lets send the ajax request to clockin.. 
     $.ajax({ 
      url: $(this).attr('href'), 
      type: "POST", 
      data: "url="+document.URL, 
      success: function(response) { 
       location.reload(true); 
       return false; 
      } 
     }); 
     return false; 
    }) 

的问题是,即使刷新页面,该会话不更新,直到我刷新页面再次手动...任何想法,我做错了什么?

回答

0

您使用location.reload(true);应忽略浏览器的缓存...

什么可能发生的情况是,当您访问的页面,你的行动的火灾之一更改会话的状态。

例如:访问页面(将操作设置为X)。 AJAX请求:设定动作为Y. 刷新:设置动作为X. 再次刷新:设置动作为Y.

对于一些调试的帮助,你可以有AJAX请求返回会话的当前状态,所以你可以看到它改变了吗?

+0

问题在于,当你重新加载会话时,会话没有被设置..当页面重新加载时,它会被设置...因此,发送会话的当前状态将在#每次都返回...其中#是1 2或3 – Johnny 2012-04-05 16:18:09