Asped
Asped

Reputation: 3103

Set an array session variable in Yii

I need to to sth like this:

$_SESSION['key']['subkey1'] = 'value1';
$_SESSION['key']['subkey2'] = 'value2';

If I do it the Yii way, I get an error: Indirect modification of overloaded element of CHttpSession has no effect

Yii::app()->session['key']['subkey1'] = 'value1';
Yii::app()->session['key']['subkey2'] = 'value2';

I could probably do sth like this:

Yii::app()->session['key'] = array('subkey1' => 'value1', 'subkey2' => 'value2')

This would be OK, but the problem still is that I cannot modify the values

Yii::app()->session['key']['subkey2'] = 'value3';

Any ideas? Is there actually any difference in using $_SESSION and Yii::app()->session ?

Upvotes: 0

Views: 865

Answers (1)

smsalisbury
smsalisbury

Reputation: 365

Try something like this

$temp = Yii::app()->session;
$temp['key']['subkey1'] = 'value1';
$temp['key']['subkey2'] = 'value2';
Yii::app()->session = $temp;

It's a workaround for the overloading error.

You can't directly modify an overloaded element because they don't really exist like properties do. You can read more about it here: http://php.net/manual/en/language.oop5.overloading.php

And you can read more about the differences between $_SESSION and Yii::app()->session here: http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/

Upvotes: 1

Related Questions