Set Session Array in Phalcon 2

Hey Guys,

I just upgraded my Project from Phalcon 1.3.2 to Phalcon 2 (Got a new Server so why use old stuff? :D)

Everything works fine, except my Session.

I set my session like this:

$this->session->set('data', array(
    'time' => time(),
    'plz' => $planet->getPlz(),
    'design' => $user->getDesign(),
    'alliance' => $user->getAllianceid(),
    'back' => NULL
));

To access one of these I use this:

$this->session->get('data')['time'];

But how can I change my variables?

$this->session->set('data['time']', time()); // ???


83.4k

I didn't know Phalcon allowed to do that in 1.3.x, Phalcon 2 return all values by value and not by reference so this is pretty much an undefined behavior which probably will not be ported to 2.0

Ok. In phalcon 1.3.x I could do it like this:

    $this->session->data['time'] = time();

So I have to rewrite session :(



44.6k
edited Nov '14

You could add a method that descends down into the session data if there are more than two parameters and otherwise calls the default. So you could write:

$this->session->update('data', 'time', time());

It wouldn't be real efficient but it would then be minimal work to change your app logic.

Or perhaps:

$this->session->update(['data', 'time'], time());


44.6k
edited Nov '14

Here is something similar for Javascript:

Journal.prototype.add = function ()
{
    var argc = arguments.length;
    if (argc < 2)
    {
        console.log('Error: Journal:add must have at least two arguments');
        return this;
    }

    var dataPtr = this.data;
    for (var i = 0; i < argc - 2; i++)
    {
        arg = arguments[i];
        if (! _.has(dataPtr, arg))
            dataPtr[arg] = {};

        dataPtr = dataPtr[arg];
    }

    dataPtr[arguments[i]] = arguments[++i];

    return this;
}

The difference is that inside the PHP function you will first need to obtain the by-value session data for that key and then you will need to shove that whole array back in at the end.

Yeah I just thought about writing my own function by implementing it into Session.zep

Something like this, don't know if this works:

    public function setArray(array! keys, string value)
    {
        //do some stuff
    }


3.6k

I also have this problem,in 1.3.4 I can change the array value which store in session, but 2.x can't do this.