2012-08-22 42 views
1

我創建了一個需要會話變量的頁面,該會話變量將被插入到表單中。我遇到的問題是我的Session->read();將變量放在我的表單的右側字段中。cakephp讀取會話變量

這裏是哪裏會話變量讀取其中的會話變量(最後一個)在此函數創建

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.'); 
    } 




    } 
} 

這裏的形式我嘗試輸入變量$持續到

<?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。

回答

2

您沒有將變量$last設置爲您的add_again()方法。只需在下面的行中讀取會話中的值。

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

知道這是愚蠢的。 – user1393064