多个复选框,结果存储在相同的DB字段中 - cakePHP

时间:2012-11-20 15:37:04

标签: cakephp

作为CakePHP(2.2)应用程序的一部分,我在用户控制器admin_add()中编写,我需要有一系列与用户可以访问的文档相关的复选框。但是,我需要每个复选框的结果进入相同的DB字段。

例如,假设您有3个复选框,其值为1,2和& 3 - 如果所有三个都被勾选,它们将进入与以下字符串相同的db字段:

1,2,3

我该怎么做?

我知道这很麻烦,通常我会用ACL控制这个,但是需要不同地做这件事(主要是因为这个应用程序的前一个版本的编写方式以及我无法控制的其他因素)。

提前致谢

更新大卫的评论**

我对您建议的代码进行了略微修改,如下所示:

if(isset($this->request->data['User'])){
    foreach($this->request->data['User']['manuals'] as $key => $value) {
    if ($value == 1) {
        $field .= $key .", ";
    }
}
    $this->request->data['User']['manuals'] = rtrim($field, ', ');
}

这段代码可以很好地运行控制器add()函数(根据需要将正确的字符串写入db),但在beforeSave()模型函数中完成后会产生以下错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

导致上述错误的SQL语句是:

SQL Query: INSERT INTO `cakeapp`.`users` (`webforms_email`, `manuals_email`, `user_status_id`, `view_uat`, `username`, `password`, `forename`, `surname`, `company`, `position`, `version_numbers_id`, `support_case_reference`, `support_web_password`, `group_id`, `manuals`, `modified`, `created`) VALUES ('1', '1', 2, '1', 't800@cyberdyne.com', '48e813c14688aff66054d1f1eb93c02d977648c1', 'T800', '101', 'Cyberdyne', 'terminator', 1, '', '', 14, Array, '2012-11-21 12:28:33', '2012-11-21 12:28:33')

问题似乎是它试图插入的数组而不是数组的内容。有任何想法吗?再次感谢您的帮助。

按要求 -

public function beforeSave() {

    // Added this if for cases when user being saved but password isnt set 
    if (isset($this->data['User']['password'])) {       
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        return true;
    }


    if(isset($this->request->data['User'])){
        foreach($this->request->data['User']['manuals'] as $key => $value) {
            if ($value == 1) {
                $field .= $key .", ";
            }
        }
        $this->request->data['User']['manuals'] = rtrim($field, ', ');
    }


}

调试$ this-> request-> add()上的数据:

array(
'User' => array(
    'password' => '*****',
    'username' => 'test@test.com',
    'forename' => 'James',
    'surname' => 'Jacobs',
    'company' => 'Company name',
    'position' => 'Web dev',
    'version_numbers_id' => '1',
    'support_case_reference' => '',
    'support_web_password' => '',
    'webforms_email' => '0',
    'manuals_email' => '0',
    'group_id' => '14',
    'user_status_id' => '1',
    'view_uat' => '1',
    'manuals' => array(
        (int) 36 => '1',
        (int) 31 => '1',
        (int) 32 => '1',
        (int) 33 => '1',
        (int) 34 => '0',
        (int) 35 => '0',
        (int) 37 => '0',
        (int) 38 => '0',
        (int) 39 => '0',
        (int) 40 => '0',
        (int) 41 => '0',
        (int) 30 => '0',
        (int) 29 => '0',
        (int) 24 => '0',
        (int) 25 => '0',
        (int) 26 => '0',
        (int) 27 => '0',
        (int) 28 => '0',
        (int) 47 => '0',
        (int) 48 => '0',
        (int) 49 => '0',
        (int) 118 => '0',
        (int) 117 => '0',
        (int) 94 => '0',
        (int) 88 => '0',
        (int) 51 => '0',
        (int) 50 => '0',
        (int) 46 => '0',
        (int) 45 => '0',
        (int) 9 => '0',
        (int) 6 => '0',
        (int) 5 => '0',
        (int) 10 => '0',
        (int) 4 => '0',
        (int) 44 => '0',
        (int) 7 => '0',
        (int) 43 => '0',
        (int) 42 => '0',
        (int) 125 => '0',
        (int) 124 => '0',
        (int) 127 => '0',
        (int) 129 => '0',
        (int) 130 => '0',
        (int) 126 => '0',
        (int) 131 => '0',
        (int) 132 => '0',
        (int) 133 => '0',
        (int) 134 => '0',
        (int) 135 => '0',
        (int) 136 => '0',
        (int) 137 => '0',
        (int) 168 => '0',
        (int) 123 => '0',
        (int) 128 => '0',
        (int) 97 => '0',
        (int) 91 => '0',
        (int) 101 => '0',
        (int) 100 => '0',
        (int) 98 => '0',
        (int) 92 => '0',
        (int) 95 => '0',
        (int) 93 => '0',
        (int) 74 => '0',
        (int) 120 => '0',
        (int) 73 => '0',
        (int) 75 => '0',
        (int) 76 => '0',
        (int) 72 => '0',
        (int) 122 => '0',
        (int) 119 => '0',
        (int) 175 => '0',
        (int) 174 => '0',
        (int) 173 => '0',
        (int) 172 => '0',
        (int) 171 => '0',
        (int) 170 => '0',
        (int) 169 => '0',
        (int) 176 => '0'
    ),
    'filename' => array(
        'name' => '',
        'type' => '',
        'tmp_name' => '',
        'error' => (int) 4,
        'size' => (int) 0
    )
)

1 个答案:

答案 0 :(得分:1)

如果我这样做,我会使用模型回调函数。

User模型中,我会创建一个beforeSave()。您的实现可能会有所不同,但希望您从下面的快速示例中了解到这一点。

public function beforeSave(){
  if(isset($this->data['User']){
    foreach($this->data['User']['documents'] as $doc){
       $field .= $doc .", ";
    }
    $this->data['User']['allowed_docs'] = rtrim($field, ', ');
  }
}