Thursday 19 May 2011

CakePHP - Matching Multiple Checkbox Inputs With Join/Cross Tables Without Magic

Given the situation: A `trainee` wants to select available time for courses. We have Table `days` and Table `sessions`, having a join table `days_sessions`. Each `days` may have 3 `sessions`, and  can select multiple `days` in the form. In the end, we end up with another join table like `trainees_days_sessions`

1. We have predefined data in `days_sessions`, e.g. Monday morning - 'MonM', Tuesday night - 'TueN', etc.
2. In the form we have checkboxes for each `days_sessions`. We may code it as

Input type='checkbox' id='data[DaySession][DaySession][]' name='data[DaySession][DaySession][]' value='*'/

Note that the `id` must be coded in such a way of data[model_name][model_name][] (as defined in your model), while the value (* sign) must be the `id` column in `days_sessions` table.

No comments: