2012-08-16 26 views
4

試圖創建一個表單和確認頁面,詢問確認用戶是否想與用戶建立關係。該表格只有一個字段,其中包含一個id並將用戶引導至確認頁面。確認頁面有兩個按鈕,確認和拒絕,當用戶點擊確認表單將被提交給數據庫時,如果用戶點擊否認表單中的數據將被丟棄。確認頁面還將打印他們正在添加的用戶的用戶名。Cakephp從另一個視圖檢索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')); 


    ?> 

回答

4

的ID添加到重定向:

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

要成爲類似:

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

謝謝,但仍然無法正常工作。我使用params嗎?或者我應該使用params或不? – user1402677 2012-08-16 03:02:23

+0

這是一個正確的方法;雖然您可能想要傳遞'$ account_id'而不是'receiver_id',否則您必須在其他地方遇到問題。 – Ross 2012-08-16 07:43:18

+0

爲什麼羅斯? – user1402677 2012-08-16 08:15:55

0

您可以直接訪問$ id,而無需使用$ this-> request-> params。你可以使用它到你的confirm()方法中。

+0

請問是否它不適合你。 – 2012-08-16 04:30:23

+0

Nah也沒有工作。 $ id和$ compName都返回空數組。我想我無法從數據庫中獲取價值。我怎麼會這樣做呢? $ compName應該從賬戶表中獲取company_name。 $ id是關係表中的receiver_id。 – user1402677 2012-08-16 05:19:55