This happens because of the way HABTM works within CakePHP - it will only save HABTM data if the HABTM key exists in the array. Hence, when no checkboxes are checked, there is no data passed through and cake doesn't touch your existing habtm records.
A quick fix would be to add a few lines of code to your controller.
if (!empty($this->data)) {
if (empty($this->data['buddy'])) {
$this->data['buddy'] = array('buddy' => array(''));
}
if ($this->Participant->saveAll($this->data)) {
// ...
} else {
// ...
}
}
However, if might also be possible for you to use cake's form helper (instead of the other helper you are using) to do this in your view:
echo $form->inputs(array(
'legend' => 'Nominate your artwork for awards',
'buddy' => array('label' => false, 'multiple' => 'checkbox', 'options' => $allBuddies)
));
iFunk
2009-12-15 13:23:06