質問

My problem: I can't access session data in the view, which I tried to save in the controller before rendering the view. In my opinion there's an error when storing the session data. (It's necessary for me to modify the session data after creating it, not only in the single action.)

ProcessController.php

public function actionNew() {

    $formhash = md5(time());
    $hashList = array();
    $hashList[$formhash]['processingNumber'] = '';
    //fill with empty model
    $hashList[$formhash]['model'] = $this->loadModel();
    //store hashList in session
    Yii::app()->session['hashList'] = $hashList;

    $this->render('/process', array('hashValue => $formHash));

}

Now in the view I need the data from the session to show them to the user. But when dumping the hashList it just dumps "null" (Maybe because the saving in the controller didn't went well).

process.php

<?php
$form = this->beginWidget('CActiveForm', array(
    'id' => 'process_form',
    //several other things...
));
//Output: null
CVarDumper::dump(Yii::app()->session['hashList'],10,true);
?>

I tried to use $_SESSION instead of Yii::app()->session, which gives me access to the data in the view. But when handling other actions in the Controller the $_SESSION variable is undefined.

Any suggestions?

Thank you.

役に立ちましたか?

解決

Long answer is:

Regarding to this documents:

$session=new CHttpSession;
$session->open();
$value1=$session['name1'];  // get session variable 'name1'
$value2=$session['name2'];  // get session variable 'name2'
foreach($session as $name=>$value) // traverse all session variables
$session['name3']=$value3;  // set session variable 'name3'

You can use as well:

Yii::app()->session->set('hashList', $hashList);
Yii::app()->session->get('hashList');

And set it again.

Beside this session thing, why do not you use this:

$this->render('/process', array('hashValue => $formHash, 'hashList' => $hashList));

So you do not need to save it in a session if you can reach it directly into the view.

他のヒント

According to the documentation your code should work. An alternative might be to use the following, but it does the same:

Yii::app()->session->add('hashList', $hashList); // set the value
$hashList = Yii::app()->session->get('hashList'); // get the value

I expect the problem either a debugging problem, meaning you have observed cached or otherwise outdated data or a problem in parts of your code that you have not shown.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top