フォームに挿入されるセッション変数を必要とするページを作成しました。私が抱えている問題はSession->read();
、変数をフォームの正しいフィールドに入れていることです。
これは、セッション変数(最後)が作成されるコントローラーです
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')); ?>
最初の関数では、セッション変数が最後に作成された場所です。その変数をビューに入れようとしています。