I have built a CakePHP app for creating appointments against clients and everytime I try to create an appointment for a client it always overwrites any existing appointment with the same ID of 1. The ID is set to AI in the database so it should auto increment but it’s not.
Here is my controller action:
function admin_add($client_id)
{
$this->set('client_id', $client_id);
$this->set('client', $this->Appointment->Client->find('first', array('conditions' => array('Client.id' => $client_id))));
$this->set('doctors', $this->Appointment->Doctor->find(
'list',
array(
'fields' => array('Doctor.name'),
'order' => array('Doctor.firstname', 'Doctor.lastname')
)
));
if (!empty($this->data))
{
if ($this->Appointment->save($this->data))
{
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('admin' => true, 'controller' => 'appointments', 'action' => 'index'));
}
}
}
and this is my model:
class Appointment extends AppModel
{
var $name = 'Appointment';
var $useTable = 'appointments';
var $belongsTo = array('Client','Doctor');
}
and finally my view for the form:
<h1>Book Appointment <em>for</em> <?php echo $client['Client']['firstname'] . " " . $client['Client']['lastname']; ?></h1>
<?php echo $this->Form->create('Appointment', array('url' => array('admin'=>true,'controller'=>'appointments','action'=>'add',$client_id))); ?>
<fieldset id="post-form">
<ul>
<li>
<?php echo $this->Form->input('client_id',array('type'=>'hidden','value'=>$client_id)); ?>
<?php echo $this->Form->input('doctor_id',array('label'=>'<strong>Choose Doctor</strong>'),$doctors); ?>
</li>
<li>
<?php echo $this->Form->input('datetime',array('label'=>'<strong>Date and Time</strong>')); ?>
</li>
<li>
<?php echo $this->Form->input('treatment',array('label'=>'<strong>Treatment</strong>')); ?>
</li>
<li class="sep clearfix">
<input class="submit" type="submit" name="submit" value="Book Appointment" />
<?php echo $this->Html->link('Cancel',array('admin'=>true,'controller'=>'clients','action'=>'view',$client_id)); ?>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
Adding this to the view fixes the problem:
<?php echo $this->Form->input('id', array('type' => 'hidden')); ?>but why do I need to do this? As clearly the database was able to fill the field with an integer it just didn’t auto increment :/