使用cakephp将复选框保存到单独的表中

时间:2016-07-21 16:19:08

标签: php cakephp checkbox has-and-belongs-to-many

我有空格来容纳会产生费用的事件和物品。

在后端,我想添加一个空格和可能的费用,因此我有一个表单,其中包含多个选项复选框。

我希望它将事件空间ID写入facility_event_spaces_id列,并将费用写入我的' event_charges'中的facility_event_charges_id列。表

它没有保存任何东西到我的桌子,event_charges。 我可能在模型或控制器中有两个不正确的东西。任何cakephp大师都有建议吗?

我们正在运行cakephp 1.3。谢谢!

<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

添加表单的源视图

<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>

我创建的joinTable CREATE TABLE event_chargesid int(11)NOT NULL,   facility_event_spaces_id int(11)NOT NULL,   facility_event_charges_id int(11)NOT NULL,   主要关键(id) );

我的模特

<?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
?>

我的控制器 - facility_event_spaces_controller.php

功能添加

$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;
                } 
}

另外,这里是facility_event_charges表

enter image description here

1 个答案:

答案 0 :(得分:0)

我发现将数据保存到单独表格的一种方法是:

$maxId = $this->FacilityEventSpace->getMaxId();
                    // routines for Charges
                    $charges = $this->data['FacilityEventSpace']['charge'];
                    $n = 0;
                    foreach ($charges as $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