我在 Cakephp 表单中有一个复选框,可以有多个值。视图中:
<?php // Multiple checkbox form
echo $this->Form->input('report_types', array(
'type'=>'select',
'label'=>'Report Type(s)',
'multiple'=>'checkbox',
'options'=>array(
'option 1'=>'option 1',
'option 2'=>'option 2',
'option 3'=>'option 3',
),
)); ?>
当我将其加载到数据库中时,它返回“未找到列:1054“字段列表”中的未知列“数组””错误,因为它试图在应该是字符串的位置添加数组。
我尝试过转换any of the $this->request->data
它以数组形式转换为字符串,但这会干扰我之前在表单中选择的日期(它们也存储为数组)。
我还尝试将多个复选框字段的值转换为beforeValidate()
模型中的方法,但它需要太多重复,并且当我需要“解压”数据时会变得混乱:
<?php class Request extends AppModel {
...
function beforeValidate() {
if(!empty($this->request->data['Request']['attachment_types'])) {
$this->data['Request']['attachment_types'] = implode(',', $this->data['Request']['attachment_types']);
}
if(!empty($this->request->data['Request']['report_types'])) {
$this->data['Request']['report_types'] = implode(',', $this->data['Request']['attachment_types']);
}
// Am I going to have to keep adding if-statements here?
}?>
此外,!empty()
方法不起作用,因为该字段永远不会为空(由于 CakePHP 进行复选框输入时自动创建的隐藏字段)。
有谁知道如何将多个复选框输入提交到数据库中?这似乎是一个相当温和的请求... CakePHP 在这方面有任何“自动魔法”技能吗?