CakePHP:将多个复选框输入提交到数据库 [英] CakePHP: Submitting a multiple checkbox input into the database
问题描述
我有一个复选框在Cakephp形式,可以有多个值。在视图中:
I have a checkbox in a Cakephp form that can have multiple values. In the view:
<?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',
),
)); ?>
当我加载到数据库时,它返回一个Column not Found:1054 Unknown column'Array'在'字段列表'错误,因为它试图添加一个数组,它应该是一个字符串。
When I load this into the database it returns a "Column not found: 1054 Unknown column 'Array' in 'field list'" error because it is trying to add an array where it should be a string.
我试过转换任何 $ this-> request-> data
中以数组形式转换为字符串,但干扰日期选择我在表单中早些时候
I've tried converting any of the $this->request->data
that is in array form into a string, but that interferes with the date selects that I have earlier in the form (they are stored as arrays as well).
我也试过将多个复选框字段的值转换为中的字符串,在模型中的beforeValidate()
方法,但是它需要太多的重复,并且当我需要'unpack'数据时会变得麻烦:
I've also tried converting just the value of the multiple check-box field into a string in the beforeValidate()
method in the model, but it requires too much repetition and gets messy when I need to 'unpack' the data:
<?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进行复选框输入时自动创建的隐藏字段)。
Besides, the !empty()
method doesn't work because the field will never be empty (due to the hidden field automatically created when CakePHP makes a checkbox input).
任何人都有任何想法我可以提交多个复选框输入到数据库?这似乎是一个相当温和的要求... CakePHP在这方面有什么自动化技能吗?
Does anyone have any ideas as to how I can submit a multiple check-box input into a database? It seems like a pretty modest request...does CakePHP have any "automagic" skills in this respect?
推荐答案
,我将数组中的数据类型更改为在我的数据库中具有定义为('选项1','选项2','选项3')
的选项的集合。如果你的数据库中没有'set'数据类型,没关系。然后,我修改了 beforeValidate()
函数来序列化数据。
To solve this, I changed the datatype in the array to a set with the options defined as ('option 1','option 2','option 3)
in my database. If you don't have a 'set' datatype in your database, it's okay. Then, I amended my beforeValidate()
function to serialize the data.
<?php class Request extends AppModel {
...
function beforeValidate() {
if($this->request->data['Request']['attachment_types']!=0) {
$this->data['Request']['attachment_types'] = implode(',', $this->data['Request']['attachment_types']);
}
if($this->request->data['Request']['report_types']!=0) {
$this->data['Request']['report_types'] = implode(',', $this->data['Request']['report_types']);
}
// Yes, I will just have to keep adding if-statements :(
}?>
注意,在上面的例子中,我有两个带有复选框('report_types'和'attachment_types')的字段,它解释了为什么我有两个if语句。
Note in the above example I had two fields with checkboxes ('report_types' and 'attachment_types') which explains why I had two if-statements.
感谢JvO的帮助!
这篇关于CakePHP:将多个复选框输入提交到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!