使用 cakephp 将复选框保存到单独的表中
Posted
技术标签:
【中文标题】使用 cakephp 将复选框保存到单独的表中【英文标题】:Save checkboxes to separate table using cakephp 【发布时间】:2016-07-21 16:19:08 【问题描述】:我有一些空间可以存放会产生费用的活动和物品。
在后端,我想添加一个空格和可能的费用,所以我有一个带有多个选择复选框的表单。
我希望它将事件空间 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>
添加表单源视图
<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
创建表event_charges
(
id
int (11) 非空,
facility_event_spaces_id
int(11) 非空,
facility_event_charges_id
int(11) 非空,
主键 (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 表
【问题讨论】:
【参考方案1】:我发现将数据保存到单独表格的一种方法是:
$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
【讨论】:
由于我的复选框来自一个单独的表,'facility_event_charges' 和那些被“选中”的被保存到另一个表中,'event_charges' 我如何在我的 edit.ctp 文件中呈现,所有复选框,包括那些被选中的复选框?以上是关于使用 cakephp 将复选框保存到单独的表中的主要内容,如果未能解决你的问题,请参考以下文章