Question

I have a hasAndBelongsToMany association between Node and NodeTag.

My save works great when at least one is selected, but - if there is a previous association, and they try to save with none selected, my habtm table doesn't get updated, since it's not getting anything passed for NodeTag.

(I'm not sure if this is because I'm using javascript and custom buttons, or if the default CakePHP checkboxes do the same thing).

Was it helpful?

Solution

In my Controller, before the save, I added this:

if(!isset($this->request->data['NodeTag'])) {
    $this->request->data['NodeTag'][0] = array();
}

This makes it so if I wasn't sending any NodeTag data, I now pass an empty array, and it updates the habtm table so that this Node no longer has any rows for NodeTags.

Note: Notice the array structure: ['NodeTag'][0] = array();

OTHER TIPS

I was actually using your solution until it dawned on me that it is possible to achieve this with the Formhelper. Setting the hiddenField option to true ('hiddenField' => true), will force the submission of an empty array. Doing that saves you the trouble of checking once more in the controller.

I do not know how you could achieve it with javascript, but the default checkboxes have the functionality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top