As part of a CakePHP (2.2) app I’m writing in my user controllers admin_add() I need to have a series of check boxes relating to what documents a user can access. However, I need the results of each check box to go into the same DB field.
For example, say you had 3 checkboxes with the values 1, 2 & 3 – if all three were ticked they would go into the same db field as the following string:
1,2,3
How can I do this?
I know this is messy and normally I’d have this controlled by ACL but the need has arisen to do this differently (mainly due to the way the previous incarnation of this app was written and other factors outside of my control).
Thanks in advance
Update re David’s comment **
I’ve got a slightly amended version of your suggested code as below:
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 code works in fine the controller add() function (writes the correct string to the db as I wanted) but produced the following error when done in the beforeSave() model function
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
The SQL statement that causes the above error is :
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')
The issue seems to be with the Array its trying to insert rather than the contents of the array. Any ideas? Thanks again for your help.
As requested –
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, ', ');
}
}
Debug of $this->request->data on 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
)
)
)
If I was doing this I would use a model callback function.
In your
Usermodel, I’d create abeforeSave(). Your implementation might differ, but hopefully you get the idea from the below quick example.