首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >symfony2会话需要2次刷新才能更新

symfony2会话需要2次刷新才能更新
EN

Stack Overflow用户
提问于 2012-04-04 04:43:03
回答 1查看 537关注 0票数 0

我创建了一个在每个页面上运行的控制器,它正在工作,除了一个方面。

在我的base.html.twig文件中,我放置了:{% render "EcsCrmBundle:Module:checkClock" %}

因此,我有一个名为ModuleController的控制器,其操作为checkClockAction,其中包含以下代码:

代码语言:javascript
复制
<?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提交的:

代码语言:javascript
复制
$(".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;
    })

问题是,即使页面刷新,会话也不会更新,直到我再次手动刷新页面...你知道我做错了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2012-04-05 23:06:53

您正在使用location.reload(true);,它应该忽略浏览器缓存...

可能发生的情况是,当您访问页面时,会触发您的一个操作来更改会话的状态。

例如:访问页面(将action设置为X)。AJAX请求:将action设置为Y。Refresh:将action设置为X。再次刷新:将action设置为Y。

对于一些调试帮助,您可以让AJAX请求返回会话的当前状态,以便在更改时看到它吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10001083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档