0
votes

Our group create a database for patron to attend an event (booking) which has lots of entities.

We have three tables: patron, booking, booking_patron (join table).

We want to create 2 pages in the view (add.ctp and add2.ctp) of booking_patron.

Page add.ctp has a drop down box which lists booking ID and a submit button. Page add2.ctp should appear the booking ID that we chose before (in add.ctp). Also in add2.ctp, we did make a function that user can create a patron and assign that patron to the booking 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.'));
        }
    }
}

The problem is: we don't know how to get the booking ID from add.ctp to add2.ctp. At the moment, we use a drop down box that list booking ID in add2.ctp to choose the booking; we want to change that to a static text field that appears the booking ID from add.ctp.

We also have a function that can create a new patron in add2.ctp. We want to assign the new patron to the chosen booking ID from add.ctp

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
You could temporary save it in the session; $this->Session->write('booking_id', $id); - noslone
ty for the solution, but i just dont get it (im new). where should i add this code? is it add.ctp or add function in controller - my love
In that case you should read the documentation - noslone

1 Answers

0
votes

This should work

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'));
}

And to retreive the ID use this in your add2() function

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

Update

edit add.ctp -- This needs to be a form, not just a link, otherwise you don't submit the booking ID

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