Take the 2-minute tour ×

I have table user which have fields username, password, and type. The type can be any or a combination of these: employee, vendor and client e.g. a user can be both a vendor and a client, or some another combination. For the type field I have used the multiple checkbox (see the code below). This is the views/users/add.ctp file:

<div class="users form">
        <legend><?php __('Add User'); ?></legend>

        echo $this->Form->input('type', array('type' => 'select', 'multiple' => 'checkbox','options' => array(
     'client' => 'Client',
     'vendor' => 'Vendor',
     'employee' => 'Employee'


In the model file I have used a beforeSave callback method:


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 values in the database.

The main problem comes when I'm editing a user. If a user has selected multiple types during user creation, none of the checkboxes is checked for the type.

share|improve this question
You have the data modeled wrong to begin with. Before you invest time on code for this model, get that part right. User->Type is a one-to-many relation. That type of relation is represented with a second table where the user may have between 1 and 3 rows. If you ever have multiple values in a single column (i.e. column values are not atomic) you've not normalized your design. –  Dan Grossman Feb 8 '11 at 9:08
@Dan: You want to say that I have to create new table type and user_type tables to relate the types with users. –  samir chauhan Feb 8 '11 at 9:14
You don't necessarily need a types table, but a user_type table, yes. –  Dan Grossman Feb 8 '11 at 9:15
You could, of course, just have three boolean fields, i.e. is_client, is_vendor, is_employee. And handle that with three checkboxes in the form. –  Leo Feb 8 '11 at 9:59
@Dan In user_type table there would be 3 fields id, typeName and user_id. or u have the other way. Could u explain a bit more abt the table structure. –  samir chauhan Feb 8 '11 at 11:18
add comment

2 Answers

up vote 0 down vote accepted

you have $this->data['User']['type'] = join(',', $this->data['User']['type']); in the beforeSave() so you would need to do the same in afterFind() $this->data['User']['type'] = explode(',', $user['User']['type']); which would make it an array again.

This is however a horrible design, you should consider doing it properly.

share|improve this answer
add comment

@dogmatic69 after following this $this->data['User']['type'] = explode(',', $user['User']['type']); and the array is properly arranged the checked box is still unchecked

share|improve this answer
add comment

Your Answer


