jeudi 21 juillet 2016

Save HABTM checkboxes to join table using cakephp

I have spaces that hold events and items that would incur charges.

On the back-end, I want to add a space and the possible charges so I have a form with multiple select checkboxes for those charges.

I want it to write the event space ID to the facility_event_spaces_id column and write the charges to facility_event_charges_id column in my 'charges_to_events' table.

It's not saving anything to my joinTable, charges_to_events. I may have something incorrect in the model or controller or both.


<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 I created CREATE TABLE charges_to_events ( facility_event_spaces_id int(11) NOT NULL, facility_event_charges_id int(11) NOT NULL, PRIMARY KEY (facility_event_spaces_id,facility_event_charges_id) );


my Models

<?php
class FacilityEventSpace extends AppModel {
var $name = 'FacilityEventSpace';
var $hasAndBelongstoMany = array('FacilityEventCharge' =>
                        array('className'    => 'FacilityEventCharge',
                              'joinTable' => 'charges_to_events',
                              'foreignKey'   => 'facility_event_spaces_id',
                              'associationForeignKey' => 'facility_event_charges_id',
                              'with' =>'ChargeToEvent'
                        ),
                  );
} // end Model
?>


<?php
class FacilityEventCharge extends AppModel {

var $name = 'FacilityEventCharge';

} // end Model
?>


<?php
class ChargesToEvent extends AppModel
{
 var $name = 'ChargesToEvent';                      
} // 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 the facility_event_charges table

enter image description here




Aucun commentaire:

Enregistrer un commentaire