1

我创建了一个需要将会话变量插入表单的页面。我遇到的问题是我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 中。

4

1 回答 1

2

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

 $last = $this->Session->read('last');
 $this->set('last', $last); // write this line just below the above line.
于 2012-08-22T07:55:39.767 回答