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 8639665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:04:35+00:00 2026-06-12T11:04:35+00:00

I have two models with the relationship Advisor belongsTo Room , Room hasMany Advisor

  • 0

I have two models with the relationship Advisor belongsTo Room, Room hasMany Advisor. Advisor has a foreignKey constraint in the database (Advisor.room_id) which points to a specific room. The default value for this is the NULL value (representing an advisor without a room).

Suppose I had an Advisor, with room_id set to n. I now wish to unassign the the nth room from Advisor – using a select field, I can reset room_id to NULL, with the following request->data structure:

[Room] => Array
    (
        [name] => TestRoom2
        [type] => single suite
        [id] => 4
    )

[Advisor] => Array
    (
        [0] => Array
            (
                [id] => 14
                [room_id] => 
                [name] => foo
            )
    )

However, when I attempt to do this through the use of checkboxes with the same generated $this->request->data, MySQL refuses to update the NULL value.

In addition, it seems that changing the value of room_id explicitly in request->data in the second case has no effect. However, if I were to change Advisor.0.name to hax, (by modifying request->data directly) the name field does save.

I save via calling $this->Room->saveAll($request->data, array('deep' => true)) – this is true both in the case of the select field and the checkbox.

I am generating the series of checkboxes by repeatedly calling the Form helper:

$count = 0;
// $key is the Advisor id, and $attributes is an array of the form
//      array('name' => (string), 'disabled' => (bool))
foreach($options['advisorList'] as $key => $attributes) {
    $form[] = $this->Form->hidden(sprintf('Advisor.%s.id', $count), array('value' => $key));
    $form[] = $this->Form->input(sprintf('Advisor.%s.room_id', $count), array(
        'type' => 'checkbox',
        'label' => $attributes['name'],
        'disabled' => $attributes['disabled']));
    $count++;
}

Moreover, if room_id has already been set to NULL, there is no problem setting room_id – except, room_id will be set regardless if the checkbox is checked or not.

Any help would be greatly appreciated – thanks!

  • 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-06-12T11:04:36+00:00Added an answer on June 12, 2026 at 11:04 am

    Solved! This has to do with funky cakePHP $model->saveAll() on hasMany relationships.

    In order to correctly update this form, all advisors that may be changed in saveAll() must unset their relationship to their respective rooms and saved in the database

    foreach($copyOfData['Advisor'] as $advisor) {
        $advisor['room_id'] = null;
        $this->Advisor->save(array('Advisor' => $advisor));
    }
    

    Only after this has been done, can we call $this->Advisor->saveAll($this->request->data, array('deep' => true)) and have saveAll() behave as expected.

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

Sidebar

Related Questions

I have two models, Project and Category, which have a many-to-many relationship between them.
I have two models that has a manytomany relationship with a 'through' table in
Let's assume I have two models: A and B. A has one-to-many relationship with
I have two models with a has-many/belongs-to relationship. The routes are nested, and I
I have two models: Plans and PlanDetails . Relationship is: PlanDetails hasMany Plans .
In this situation I have two models, Comment and Score. The relationship is defined
I have got two models, User and Event. I need two kind of relationship
I have two scaffold-generated Models, student and class . They have a many-to-many relationship
I have a relationship between two models, Registers and Competitions . I have a
I have a simple has_one/belongs_to relationship between two models. This is a new association

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.