cakephp读取会话变量

时间:2012-08-22 07:00:38

标签: session cakephp

我创建了一个页面,需要一个会插入表单的会话变量。我遇到的问题是Session->read();将变量放入表单中的右侧字段中。

这里是创建会话变量(last)的控制器

function add($last=null){

    $accounttemplates=$this->Template->find('list', array(
    'fields'=>array('name'),
    'conditions' => array(
    'account_id' => $this->Auth->user('account_id'))));


        //retrieve Account Id of current User       
        $accountid=$this->Auth->user('account_id');

        $templatefields=$this->Field->find('all', array(
        'conditions' => array(
        'Field.template_id' => "10002")));

        $this->set('accountid', $accountid); 
        $this->set('templatefields', $templatefields); 

        $this->set('accounttemplates', $accounttemplates);

    if($this->request->is('post'))
    {

    $this->Field->create(); 

    if ($this->Field->save($this->request->data))
    {   
        if($this->request->data['submit'] == "type_1") 
            {

                $last=$this->Session->write('last', $this->data['Field']['template_id']);
                $this->Session->setFlash('The field has been saved');  
                $this->redirect( array('controller' => 'fields','action' => 'add_again',$last));
            } 
            if($this->request->data['submit'] == "type_2") 
            { 
                $this->Session->setFlash('The template has been saved'); 
                $this->redirect( array('controller' => 'templates','action' => 'index'));
            } 


    }
    else
    {
        $this->Session->setFlash('The field could not be saved. Please, try again.'); 
    } 
 }

     $this->set('last', $last); 


  }

在此函数中是读取会话变量的地方。

function add_again($last=null){

    $accounttemplates=$this->Template->find('list', array(
    'fields'=>array('name'),
    'conditions' => array(
    'account_id' => $this->Auth->user('account_id'))));


        //retrieve Account Id of current User       
        //$accountid=$this->Auth->user('account_id');

        $templatefields=$this->Field->find('all', array(
        'conditions' => array(
        'Field.template_id' => "10002")));

        //$this->set('accountid', $accountid); 
        $this->set('templatefields', $templatefields); 
        $last=$this->Session->read('last');
        $this->set('accounttemplates', $accounttemplates);

    if($this->request->is('post'))
    {

    $this->Field->create(); 

    if ($this->Field->save($this->request->data))
    {   
        if($this->request->data['submit'] == "type_1") 
            {
                $this->Session->setFlash('The field has been saved');  
                $this->redirect( array('controller' => 'fields','action' => 'add_again'));
            } 
            if($this->request->data['submit'] == "type_2") 
            { 
                $this->Session->setFlash('The template has been saved'); 
                $this->redirect( array('controller' => 'templates','action' => 'index'));
            } 


    }
    else
    {
        $this->Session->setFlash('The field could not be saved. Please, try again.'); 
    } 




  }
}

这是我试图将变量$ last输入

的表单
<?php echo $this->Form->create('Field', array('action'=>'add'));?>
                        <td><?php echo $this->Form->input('name', array('label'=>false)); ?></td>
                        <td><?php echo $this->Form->input('description', array('label'=>false)); ?></td>
                        <td><?php echo $this->Form->input('default_value', array('label'=>false)); ?></td>
                        <td><?php echo $this->Form->input('template_id', array('value' => $last, 'type'=>'text'));?></td>
                    <?php   echo $this->Form->hidden('active',array('default'=>'1')); ?>
第一个函数中的

是最后创建会话变量的地方,我试图将视图中的变量放入template_id。

1 个答案:

答案 0 :(得分:2)

您没有将变量$last设置为add_again()方法。只需在下面的行中写下您从会话中读取值的位置。

 $last = $this->Session->read('last');
 $this->set('last', $last); // write this line just below the above line.