2013-05-21 239 views
0

我們的小組創建了一個數據庫供顧客參加一個有很多實體的事件(預訂)。Cakephp:將參數或數據傳遞給另一個頁面

我們有三個表:顧客,預訂,booking_patron(連接表)。

我們希望在booking_patron的視圖(add.ctp和add2.ctp)中創建2個頁面。

Page add.ctp有一個下拉框,其中列出了預訂ID和提交按鈕。 頁面add2.ctp應顯示我們之前選擇的預訂ID(在add.ctp中)。同樣在add2.ctp中,我們確實創建了一個函數,用戶可以創建一個贊助人並將該贊助人分配給預訂ID。

BookingsPatronsController.php

public function add() { 
    $bookings = $this->BookingsPatron->Booking->find('list'); 
    $this->set(compact('bookings', 'patrons')); 
} 
public function add2($booking_id = null) { 

    $patrons = $this->BookingsPatron->Patron->find('list'); 
    $bookings = $this->BookingsPatron->Booking->find('list'); 
    $this->set(compact('bookings', 'patrons')); 
    if ($this->request->is('post')) { 
     $this->BookingsPatron->Patron->create(); 

     if ($this->BookingsPatron->Patron->save($this->request->data)) { 
      $this->Session->setFlash("Sign In Successful"); 

      $this->BookingsPatron->create(); 

      $this->Session->setFlash(__('Well.... done')); 

      $this->redirect(array('action' => 'add3')); 
     } else { 
      $this->Session->setFlash(__('We can not add your information. Please, try again.')); 
     } 
    } 
} 

的問題是:我們不知道如何從add.ctp到add2.ctp獲得預訂ID。 目前,我們使用一個下拉框來列出add2.ctp中的預訂ID來選擇預訂;我們希望將其更改爲出現add.ctp預訂ID的靜態文本字段。

我們還有一個函數可以在add2.ctp中創建一個新的顧客。我們希望新的靠山從add.ctp分配到所選擇的預約ID

add.ctp

<?php echo $this->Form->input('Booking.booking_id');?> 
<?php echo $this->Html->link(__('Create'), array('action' => 'add3')); ?> 

add2.ctp

<?php echo $this->Form->create('BookingsPatron'); ?> 
<fieldset> 
<?php echo $this->Form->input('Booking.booking_id');?> 
<table cellpadding="3" cellspacing="3"> 
<tr> 
    <td class="heading">Name: </td> 
    <td><?php echo $this->Form->input('Patron.patrons_name', array('label' => '', 'div' => 'formLabel'));?></td> 
    <td></td><td></td> 
    <td class="heading">E-mail: </td> 
    <td><?php echo $this->Form->input('Patron.patrons_email', array('label' => '', 'div' => 'formLabel'));?></td> 
</tr> 
<tr> 
    <td class="heading">Age: </td> 
    <td><?php echo $this->Form->input('Patron.patrons_age', array('label' => '', 'div' => 'formLabel'));?></td> 
    <td></td><td></td>   
    <td class="heading">Company: </td> 
    <td><?php echo $this->Form->input('Patron.patrons_company', array('label' => '', 'div' => 'formLabel'));?></td> 
</tr> 
<tr> 
    <td class="heading">Postcode: </td> 
    <td><?php echo $this->Form->input('Patron.patrons_postcode', array('label' => '', 'div' => 'formLabel'));?></td> 
    <td></td><td></td>   
    <td class="heading">Gender: </td> 
    <td><?php echo $this->Form->input('Patron.patrons_gender', array('label' => '', 'div' => 'formLabel', 'type' => 'select', 'options' => array('Male' => 'Male', 'Female' => 'Female')));?></td> 
</tr> 
<tr> 
    <td colspan="2">PH: 1300 7 34726</td> 
    <td colspan="3"></td> 
    <td><?php echo $this->Form->submit('Submit', array('class' => 'classSubmitButton', 'title' => 'Sbumit')); ?></td> 
</tr> 
</table> 

</fieldset> 
+1

你可以暫時保存在會議; '$ this-> Session-> write('booking_id',$ id);' – noslone

+0

ty for the solution,but I just not get it(im new)。我應該在哪裏添加此代碼?是它add.ctp或添加功能在控制器 –

+1

在這種情況下,你應該閱讀[文檔](http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#sessions) – noslone

回答

0

這應該工作

public function add() { 
    if ($this->request->is('post')) { 
     $this->Session->write('booking_id', $this->request->data['Booking']['booking_id']); 
     $this->redirect(array('action' => 'add2')); // Line added 
    } 
    $bookings = $this->BookingsPatron->Booking->find('list'); 
    $this->set(compact('bookings', 'patrons')); 
} 

並且要在您的add2()函數中使用這個ID,請使用

$booking_id = $this->Session->read('booking_id'); 

更新

編輯add.ctp - 這應該成爲一種形式,而不僅僅是一個鏈接,否則你不要提交預訂ID

<?php 
    echo $this->Form->create('Booking'); 
    echo $this->Form->input('Booking.booking_id'); 
    echo $this->Form->end(__('Create')); 
?> 
+0

Ty爲你的幫助,但你能告訴我如何從add.ctp頁面鏈接到add2.ctp頁面。此刻,我用

\t 形式 - >輸入( 'Booking.booking_id');?> \t
形式 - >端( __('提交')); ?>我提交時,如何鏈接到add2.ctp。謝謝。我嘗試使用$ this-> redirect(array('action'=>'add2'));添加功能的if循環裏面 –

+0

我更新了我的答案,這是否適合你? (看到添加的行) –

+0

我試過了,那對我沒有用。當我點擊提交按鈕時,它仍然保持相同的頁面;沒有導航到add2.ctp在所有 –