Cakephp:将参数或数据传递给另一个页面

时间:2013-05-21 08:18:13

标签: cakephp parameters

我们小组为顾客创建一个数据库,以参加一个有很多实体的活动(预订)。

我们有三个表:patron,booking,booking_patron(join table)。

我们想在booking_patron的视图(add.ctp和add2.ctp)中创建2个页面。

Page add.ctp有一个下拉框,其中列出了预订ID和提交按钮。 Page 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获取预订ID到add2.ctp。 目前,我们使用下拉框列出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 个答案:

答案 0 :(得分: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'));
}

要检索ID,请在add2()函数中使用它

$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'));
?>
相关问题