1

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

例如,假设您有 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() 函数中运行良好(根据需要将正确的字符串写入数据库),但在 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, ', ');
    }


}

在 add() 上调试 $this->request->data:

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

)

4

1 回答 1

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, ', ');
  }
}
于 2012-11-20T15:44:47.653 回答