Reputation: 1547
I have a simple api that allows customers to book vehicles. In my reservations controller i have a function which is to add a reservation. Here is the code:
Controller
function add() {
if($this->request->is('post')) {
if($this->Reservation->save($this->data)) {
$this->Session->setFlash('The Reservation was successfully added!');
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash('The Reservation was not added.');
}
}
$this->set('title_for_layout','Add Reservation');
}
Here is what i have in my add view for a reservation.
View
<?php
echo $this->Form->create('Reservation', array('action'=>'add'));
echo $this->Form->input('customer_id', array( 'type' => 'text' ) );
echo $this->Form->input('vehicle_id', array( 'type' => 'text' ) );
echo $this->Form->input('date');
echo $this->Form->end('Add Reservation');
?>
But because i am adding a new reservation and i want a dropdown box for vehicle ids and one for customer ids how can i do this?
I think i have linked the models by putting this into the customers and vehicles models:
var $hasMany = array( 'Reservation' => array( 'className' => 'Reservation' ) );
Could anyone point me into the right direction so that a dropdown box would appear with a list of vehicle and customer IDs on the add reservation page.
Upvotes: 1
Views: 1229
Reputation: 386
In your add function in controller, use
$vehicle = $this->Reservation->Vehicle->find('list', array('fields' =>array('Vehicle.id','Vehicle.name')));
$this->set('vehicle', $vehicle);
In your view,use
<?php echo $this->Form->input('vehicle_id', array('type' => 'select',
'options' => $vehicle)); ?>
Use the same thing for customers also and you will get list of customers and vehicles in dropdown.
Upvotes: 3