I have table user which have fields username,password, and type. The type can be any or combination of these employee,vendor and client i.e a user can be vendor or client both or some another combination. For type field I have used the multiple checkbox, see the code below. This is the views/users/add.ctp file
Form->create('User');?> Form->input('username'); echo $this->Form->input('password'); echo $this->Form->input('type', array('type' => 'select', 'multiple' => 'checkbox','options' => array( 'client' => 'Client', 'vendor' => 'Vendor', 'employee' => 'Employee' ) )); ?> Form->end(__('Submit', true));?>This is the code I have used in the model file. A callback method beforeSave app/models/user.php
function beforeSave() { if(!empty($this->data['User']['type'])) { $this->data['User']['type'] = join(',', $this->data['User']['type']); } return true; } This code saves the multiple values as comma separated value in db.
The main problem comes when Im editing a user. If a user has selected multiple types during user creation I can't find the checkbox checked for that user types.
$this->data['User']['type'] = explode(',',$this->data['User']['type']);
and then it should work – Jason