symfony2 sessions takes 2 refreshes to update

Go To StackoverFlow.com

0

I created a controller to run on every page and it's working except for one aspect..

in my base.html.twig file I put: {% render "EcsCrmBundle:Module:checkClock" %}

So, I have a controller called ModuleController with an action of checkClockAction which contains this code:

<?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,
        ));
    }
}

This is working fine, except for when i click on one of the login/logout buttons...

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

This is submitted via jquery ajax with:

$(".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;
    })

The problem is, even though the page refreshes, the sessions don't update until I refresh the page manually again... Any ideas as to what I have done wrong?

2012-04-03 20:43
by Johnny


0

You're using location.reload(true); which should be ignoring the browser cache...

What may be happening is that when you visit the page, one of your actions fires to change the state of the session.

For example: Visit the page (set action to X). AJAX request: set action to Y. Refresh: Set action to X. Refresh again: Set action to Y.

For some debugging help, can you have the AJAX request return the current status of the session, so you can see it as it is changed?

2012-04-05 15:06
by Nick
Problem with that is, the session doesn't get set when you clockin.. it gets set when the page reloads... So sending the current status of the session would return in# every time... where # is either 1 2 or - Johnny 2012-04-05 16:18