cakephp表单输入和url参数

时间:2012-08-11 09:01:22

标签: forms cakephp params

我有一个表单从用户那里获取一些输入然后被重定向到控制器的新页面/函数,我需要post.redirect.get param以便我可以使用id的下一页的invoice

这是第一个控制器(添加信息)

public function add(){
    $this->set('title_for_layout', 'Create Invoice');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');   
    $this->layout='home_layout';

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


        //$this->Invoice->create();
        if(($this->Invoice->set($this->request->data))){
            $this->Session->setFlash('The invoice has been saved');
            $data=$this->request->data;
            if($this->Invoice->saveAll($data,array('validate'=>false))){
            $this->Session->setFlash(__('Invoice Created'));
            $this->redirect(array(
            'controller' => 'invoices','action' => 'create',
            $this->data['invoices']['id']));
            }
    }}
    else
    {
        $this->Session->setFlash('The invoice could not be saved. Please, try again.');
    }

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

    $accounts2=$this->User->find('list', array(
    'fields'=>array('account_id'),
    'conditions' => array(
    'id' => $this->Auth->user('id'))));

    $sendername=$this->Account->find('list', array(
    'fields'=>array('company_name'),'conditions' => array(
    'id' => $accounts2)));

    $accounts=$this->User->Relationship->find('list', array(
    'fields'=>array('receiver_id'),
    'conditions' => array(
    'sender_id' => $accounts2)));

    $receivername=$this->Account->find('list', array(
    'fields'=>array('company_name'),'conditions'=>array(
    'id' => $accounts)));

    $this->set('accounts', $accounts);
    $this->set('accounts2', $accounts2);
    $this->set('sendername', $sendername);
    $this->set('receivername', $receivername);
    $this->set('template', $templates);


  }

这是视图

创建新发票

<?php


echo $this->Form->create('Invoice', array('action'=>'add'));


echo $this->Form->input('id',array('type'=>'hidden'));
echo $this->Form->input('sender_id', array('label' =>'Sender: ', 'type' => 'select', 'options' => $sendername));
echo $this->Form->input('receiver_id',array('label'=>'Receiver: ', 'type' => 'select', 'options' => $receivername));
echo $this->Form->input('active', array('label' => 'Schedule?', 'options' => array('1'=>'Send','0'=>'Schedule'), 'default'=>'1'));
echo $this->Form->input('templates_id', array('label'=>'Template: ','type'=>'select','options'=>$template));
echo $this->Form->end('Submit');


?>

这里是我想传递参数的控制器

public function create($id)
    {   
$fields = $this->Template->Field->find('all',array( 
          'conditions' => array(
          'template_id'=> $id,
         'Field.active'=>true)));
        if(empty($this->data)){
            $this->data= $this->Template->read(NULL, $id);
        } 
        else{
            if(($this->Template->save($this->data)))
            {
                $this->Session->setFlash('The template has been updated');
                $this->redirect(array('controller'=>'Templates', 'action'=>'view'));

            }
        }

     }
          $this->set('field', $fields);


    }

1 个答案:

答案 0 :(得分:0)

 public function add(){
    $this->set('title_for_layout', 'Create Invoice');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');   
    $this->layout='home_layout';

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


        //$this->Invoice->create();
        if(($this->Invoice->set($this->request->data))){
            $this->Session->setFlash('The invoice has been saved');
            $data=$this->request->data;
            if($this->Invoice->saveAll($data,array('validate'=>false))){
            $this->Session->setFlash(__('Invoice Created'));
            debug($this->Invoice->id);
            $this->redirect(array(
            'controller' => 'invoices','action' => 'create', 
            'invoice' => $this->Invoice->id));
            }
    }}
    else
    { 
        $this->Session->setFlash('The invoice could not be saved. Please, try again.');
    }

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

    $accounts2=$this->User->find('list', array(
    'fields'=>array('account_id'),
    'conditions' => array(
    'id' => $this->Auth->user('id'))));

    $sendername=$this->Account->find('list', array(
    'fields'=>array('company_name'),'conditions' => array(
    'id' => $accounts2)));

    $accounts=$this->User->Relationship->find('list', array(
    'fields'=>array('receiver_id'),
    'conditions' => array(
    'sender_id' => $accounts2)));

    $receivername=$this->Account->find('list', array(
    'fields'=>array('company_name'),'conditions'=>array(
    'id' => $accounts)));

    $this->set('accounts', $accounts); 
    $this->set('accounts2', $accounts2); 
    $this->set('sendername', $sendername); 
    $this->set('receivername', $receivername);
    $this->set('template', $templates);


  }