php - Save checkboxes to separate table using cakephp -


i have spaces hold events , items incur charges.

on back-end, want add space , possible charges have form multiple select checkboxes charges.

i want write event space id facility_event_spaces_id column , write charges facility_event_charges_id column in 'event_charges' table.

it's not saving table, event_charges. may have incorrect in model or controller or both. cakephp gurus have suggestions?

we're running cakephp 1.3. thanks!


<tr>     <td class="add">charges:</td>     <td>     <?php echo $this->form->input('charge', array('type'=>'select', 'multiple' => 'checkbox', 'options'=>$charges)); ?>     </td>              </tr> 

enter image description here


source view of add form

<div class="checkbox"><input type="checkbox" name="data[facilityeventspace][charge][]" value="1" id="facilityeventspacecharge1" /><label for="facilityeventspacecharge1">officials</label></div> <div class="checkbox"><input type="checkbox" name="data[facilityeventspace][charge][]" value="2" id="facilityeventspacecharge2" /><label for="facilityeventspacecharge2">ushers</label></div> <div class="checkbox"><input type="checkbox" name="data[facilityeventspace][charge][]" value="3" id="facilityeventspacecharge3" /><label for="facilityeventspacecharge3">additional staffing</label></div> <div class="checkbox"><input type="checkbox" name="data[facilityeventspace][charge][]" value="4" id="facilityeventspacecharge4" /><label for="facilityeventspacecharge4">special lighting</label></div> <div class="checkbox"><input type="checkbox" name="data[facilityeventspace][charge][]" value="5" id="facilityeventspacecharge5" /><label for="facilityeventspacecharge5">audio visual equipment</label></div> <div class="checkbox"><input type="checkbox" name="data[facilityeventspace][charge][]" value="6" id="facilityeventspacecharge6" /><label for="facilityeventspacecharge6">audio visual technician</label></div> 

my jointable created create table event_charges ( id int (11) not null, facility_event_spaces_id int(11) not null, facility_event_charges_id int(11) not null, primary key (id) );


my models

<?php class facilityeventspace extends appmodel { var $name = 'facilityeventspace'; var $hasandbelongstomany = array('facilityeventcharge' =>                         array('classname'    => 'facilityeventcharge',                               'jointable' => 'event_charges',                               'foreignkey'   => 'facility_event_spaces_id',                               'associationforeignkey' => 'facility_event_charges_id',                               'with' =>'eventcharge'                         ),                   ); } // end model ?> 

<?php class facilityeventcharge extends appmodel {  var $name = 'facilityeventcharge';  } // end model ?> 

<?php class eventcharge extends appmodel {  var $name = 'eventcharge';                      } // end model ?> 

my controller - facility_event_spaces_controller.php

function add

$this->set('charges', $this->facilityeventcharge->find('list', array('fields' => array('id', 'type'))  )); if (!empty($this->data)) {     if($this->facilityeventspace->save($this->data))                 {                  $this->flash('the event space has been saved.','/facilityeventspaces/');                         return;                 }  } 

also, here's facility_event_charges table

enter image description here

one way found save data separate table was:

$maxid = $this->facilityeventspace->getmaxid();                     // routines charges                     $charges = $this->data['facilityeventspace']['charge'];                     $n = 0;                     foreach ($charges $key => $cost) {                          $charge = array('eventcharge'=>array(                         'id'=>'',                         'facility_event_spaces_id'=>$maxid[0][0]['max(id)'],                         'facility_event_charges_id'=>$cost                         )                         );                         $this->eventcharge->save($charge['eventcharge']);                         $n = $n + 1;                     } // end charges 

Comments