What doesn’t work
On update form, if i change checkbox values, these habtm values don’t get saved to database.
Althought in create and update form i use the same method: $this->Employee->save($this->data) which should automatically handle HABTM data.
What works
I`v sucessfully set up HABTM Model by following this tutorial: Employees hasAndBelongsToMany Languages (i.e to specify which employee speaks which language).
I can create the relations: when creating new employee with create form, check any languages which employee knows and it sucessfully saves to database: employee goes to employee database table, language to language and the relations goes to employee_language table.
On edit form, i see which checboxes i checked while creating employee.
What i’v found to (probably) be guilty
When evaluating what data goes to database while creating and updating employee, i’v found out that $this->data variable structure differs.
On create form (check 2 languages):

"$this->data" Array [2]
Employee Array [6]
name_lv John Doe
position_lv Computer administrator
enabled 0
Language Array [1]
Language Array [2]
0 1
1 5
On update form (checked additional language, so 3 languages):

"$this->data" Array [1]
Employee Array [8]
name_lv John Doe
position_lv Computer administrator
Language Array [3]
0 1
1 5
2 4
enabled 0
id 68
Note that Languages are now accessable under $this->data["Employee"]["Language"] not like when creating $this->data["Language"]
Workaround
If i do $this->data["Language"] = $this->data["Employee"]["Language"]; then values get saved (yay), except that duplicate entries are present in employee_language HABTM table (oh no).
So which switch do i switch to make my checkboxes updateable?
More details (if you will)
(model) employee.php
class Employee extends AppModel {
var $name = "Employee";
var $hasAndBelongsToMany = array (
"Language" =>
array(
"className" => "Language",
"conditions" => array(
"Language.enabled" => "1"
)
)
);
...
}
(model) language.php:
class Language extends AppModel {
var $name = "Language";
...
}
(view) admin_create.ctp (this is create form. This snippet renders checkboxes for all available languages):
...
<?php
echo $form->input('Language',array(
'label' => 'Speaks in languages:',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $languages
)); ?>
...
(view) admin_update.ctp (I can see which languages i checked while creating employee):
...
<?php
echo $form->input('Language',array(
'label' => 'Speaks in languages:',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $languages,
'selected' => $html->value('Employee.Language')
)); ?>
...
Mark provided an answer in comments that solves the problem:
Yes, when i removed “selected” property, then suddenly i was able to do UPDATE on HABTM data.
So now my input control looks like:
Not having a way to convert comment as an answer, trying to contact @mark to post his comment as an answer, but no actions has taken, i’m answering my own question so that others can see that this question has a solution. That’s what is suggested on meta: https://meta.stackexchange.com/questions/1555/mark-a-comment-as-answer-to-a-question/1558#1558