Cakephp从另一个视图中检索id

时间:2012-08-16 02:16:07

标签: mysql forms cakephp views identifier

尝试创建表单和确认页面,要求确认用户是否想要与用户建立关系。表单只有一个字段,该字段带有一个id并将用户引导到确认页面。确认页面有两个按钮,当用户点击确认表单将被提交给数据库时确认和拒绝,如果用户点击拒绝,表单中的数据将被丢弃。确认页面还将打印他们试图添加的用户的用户名。

我似乎无法从添加页面(add_admin.ctp)中检索id。

如何从原始添加页面(add_admin.ctp)访问ID以在确认页面(confirm.ctp)中使用它?

public function add_admin() {
    $this->set('title_for_layout', 'Send A Relationship Request');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png'); 
    $this->layout='home_layout';

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

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

      $this->Relationship->save($this->request->data);
      if ($this->Relationship->validates(array('fieldList'=>array('receiver_id','Relationship.accountExists'))))
      {

        $this->Relationship->save($this->request->data);
        $this->Session->setFlash('The relationship has been saved');
        $this->redirect(array('controller'=>'Relationships', 'action'=>'confirm'));
      } else {
        $this->Session->setFlash('The relationship could not be saved. Please, try again.');
      } 
    }
    $this->set('account_id', $account_id);
  }

public function confirm($id){
        $this->set('title_for_layout', 'Relationships');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.png');   
        $this->layout='home_layout';

        //$id = $this->request->data(`Relationship.receiver_id`);
        //$id = $this->params('Relationship.id');
        $id = $this->params['Relationship']['id'];
        $compName = $this->request->data(`Account.company_name`);

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

        debug($id);
        pr($compName);
        pr($findName);

        $this->Relationship->unbindModel(array('hasMany'=>array('Invoice')));
        if ($this->request->is('get')) {
        $this->request->data = $this->Relationship->read(NULL, $id);
        }
        else 
        {
            if ($this->Relationship->save($this->request->data)) {

                //$this->Session->setFlash('You have successfully confirmed the relationship.');

                if($this->request->data['submit'] == "type_1") 
                { 
                    $this->Session->setFlash('The relationship has been confirmed');  
                    $this->redirect( array('controller' => 'fields','action' => 'add'));
                } 
                if($this->request->data['submit'] == "type_2") 
                { 
                    $this->Session->setFlash('The relationship was denied'); 
                    $this->redirect( array('controller' => 'templates','action' => 'index'));
                } 

            } else {
                $this->Session->setFlash('Unable to update your post.');
            }
        }
        $this->set('compName', $compName);
        $this->set('findName', $findName);
        $this->set('id', $id);
    }

add_admin.ctp

<?php
echo $this->Form->create('Relationship', array('action'=>'add_admin'));
echo $this->Form->hidden('sender_id',array('label'=>'Enter your eBox ID: ', 'type'=>'text', 'default'=>$account_id)); 
echo $this->Form->input('receiver_id',array('label'=>'Receiver eBox ID: ', 'type'=>'text'));
echo "<br />";
echo $this->Form->end('Submit');
?>

confirm.ctp

Do you want to create a relationship with <?php echo $findName ?> 
    <?php
    echo $this->Form->create('Relationship', array('action'=>'confirm'));
    echo $this->Form->input('id', array('type'=>'hidden'));

    echo $this->Form->button('Confirm', array('name' => 'submit', 'value' => 'type_1'));
    echo $this->Form->button('Deny', array('name' => 'submit', 'value' => 'type_2'));


    ?>

2 个答案:

答案 0 :(得分:4)

将ID添加到重定向:

$this->redirect(array('controller' => 'Relationships', 'action' => 'confirm'));

成为:

$this->redirect(array('controller' => 'Relationships', 'action' => 'confirm', $this->request->data['Relationship']['receiver_id']));

答案 1 :(得分:0)

您可以直接访问$ id,而无需使用$ this-&gt; request-&gt; params。您可以将它用于confirm()方法。