Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6564773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:00:34+00:00 2026-05-25T14:00:34+00:00

What doesn’t work On update form , if i change checkbox values, these habtm

  • 0

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):

Create form

"$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):

Update form

"$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')
));  ?>
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:00:35+00:00Added an answer on May 25, 2026 at 2:00 pm

    Mark provided an answer in comments that solves the problem:

    … you dont need to and you shouldnt use “selected” in your form
    input array.

    Yes, when i removed “selected” property, then suddenly i was able to do UPDATE on HABTM data.
    So now my input control looks like:

    <?php  
        echo $form->input('Language',array(
            'label' => 'Speaks in languages:',
            'type' => 'select',
            'multiple' => 'checkbox',
            'options' => $languages
    ));  ?>
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work? var newbody = <body><div>hello</div></body>; $(body).remove(); $(head).after(newbody); All i get after
Doesn't seem to work for me, maybe I just doing it wrong
Doesn't the space occupied by a variable get deallocated as soon as the control
Why doesn't this work (when parameter is set to 1) : SELECT * FROM
Doesn't matter what I do, I simply can't get this to play a sound
Why doesn't this work: <Style TargetType=s:Substance> <Setter Property=Template> <Setter.Value> <DataTemplate> <StackPanel> <TextBlock Text={TemplateBinding Name}/>
This doesn't work: $to = 'myemail@gmail.com'; $from = 'test@test.com'; $subj = 'test'; $message =
Why doesn't this code work? I'am using FF. <head> <script type=text/javascript> document.getElementById(someID).onclick = function(){
autowrite doesn't seem to work when switching tab in vim. Is there a way
This doesn't work. I've got an exception from SQL databse that column does not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.