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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:31:03+00:00 2026-05-13T05:31:03+00:00

I’m using a self referencing HABTM model with Participants. You sign up for an

  • 0

I’m using a self referencing HABTM model with Participants. You sign up for an event and when you log in to your reservation/profile you see a list of other participants and you can choose to add yourself and others into various groups; share hotel room, share transportation from airport etc.

What I’ve managed so far:

1) In my profile I see the list of all other participants with checkboxes. Great so far.
2) Adding another participant works fine. Next time I edit, the participant I added is shown as checked.
3) Removing another participant works fine too as long as you still have checked participants before you submit!

Again, with words:

There are 3 participants. I’m logged in as one of them, and I see the 2 other people on the participants list. I choose to check both of them. This works fine (always). Later I choose to remove one of them (by unchecking the checkbox and hitting submit). This also works fine (always). If I want to remove the last checkbox… nothing is updated (always!). What’s curious is that I can add and remove any odd combination of participants and it will always work UNLESS I choose to remove every participants in one go (removing a one and only participant is a special case of “remove all checked participants”).

As far as I know, HABTMs work by first deleting all relations, then re-saving them. I can see that in my tables when I remove, add, remove, add the same participant over and over again – the id on the HABTM table is always increasing. When I deselect all participants at once, however, the relations are not updated. The ids stay the same, so it’s like the save never happened.

This behaviour is so specific and peculiar, I have a feeling I’m missing something obvious here. Anyway, here’s the relevant code:

Model

class Participant extends AppModel {
 var $hasAndBelongsToMany = array(
  'buddy' => array(
   'className' => 'Participant',
   'joinTable' => 'participants_participants',
   'foreignKey' => 'participant_id',
   'associationForeignKey' => 'buddy_id',
   'unique' => true,
  )
 );

Controller

function edit($id = null) {
 if (!$id && empty($this->data)) {
  $this->Session->setFlash(__('Invalid Participant', true));
  $this->redirect(array('action'=>'index'));
 }
 if (!empty($this->data)) {
  if ($this->Participant->saveAll($this->data)) {
   $this->Session->setFlash(__('The Participant has been saved', true));
   $this->redirect(array('action'=>'index'));
  } else {
   $this->Session->setFlash(__('The Participant could not be saved. Please, try again.', true));
  }
 }
 if (empty($this->data)) {
  $this->data = $this->Participant->read(null, $id);
 }

 // Fetching all participants except yourself
 $allParticipants = $this->Participant->find('list', array('conditions' => array('participant.id ' => $id)));

 // Fetching every participant that has added you to their list
 $allBuddies = $this->Participant->ParticipantsParticipant->find('list', array(
  'conditions' => array('buddy_id' => $id),
  'fields' => 'ParticipantsParticipant.participant_id',
  'order' => 'ParticipantsParticipant.participant_id ASC'
 ));

 $this->set(compact('allParticipants','allBuddies'));
}

View

    echo $form->create('Participant');
    echo $associations->habtmCheckBoxes($allParticipants, $this->data['buddy'], 'buddy', 'div', '\'border: 1px solid #000;\'', '\'border: 1px solid #000;\'');
    echo $form->end('Submit');

I’m using a slightly modified helper, habtmCheckBoxes, found here: http://cakeforge.org/snippet/detail.php?type=snippet&id=190
It works like this: function habtmCheckBoxes($rows=array(), $selectedArr=array(), $modelName, $wrapTag=’p’, $checkedDiv, $uncheckedDiv) {}

  • 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-13T05:31:03+00:00Added an answer on May 13, 2026 at 5:31 am

    This happens because of the way HABTM works within CakePHP – it will only save HABTM data if the HABTM key exists in the array. Hence, when no checkboxes are checked, there is no data passed through and cake doesn’t touch your existing habtm records.

    A quick fix would be to add a few lines of code to your controller.

    if (!empty($this->data)) {
      if (empty($this->data['buddy'])) {
        $this->data['buddy'] = array('buddy' => array(''));
      }
      if ($this->Participant->saveAll($this->data)) {
       // ...
      } else {
       // ...
      }
    }
    

    However, if might also be possible for you to use cake’s form helper (instead of the other helper you are using) to do this in your view:

    echo $form->inputs(array(
        'legend' => 'Nominate your artwork for awards',
        'buddy' => array('label' => false, 'multiple' => 'checkbox', 'options' => $allBuddies)
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.