0

When a user adds an Event, I want them to be able to choose which band(s) are playing at that event. I already have my Events table and my Bands table each with HABTM model associations to the other.

On my "add event" page, I have a dropdown displaying the bands, so they can choose one.

echo $this->Form->input('Band', array('multiple'=>false, 'empty'=>true));

I would like to have a "add another band" button, and when clicked, it adds another dropdown. I think I already know how to do the dynamic field-adding thing - but - even when I try this like this: (just to see if I can get it to work)

echo $this->Form->input('Band', array('multiple'=>false, 'empty'=>true));
echo $this->Form->input('Band', array('multiple'=>false, 'empty'=>true));
echo $this->Form->input('Band', array('multiple'=>false, 'empty'=>true));

It doesn't save three rows in the bands_events HABTM table - it just saves one. And when I try to edit the event, all three select dropdowns default-select one of previously selected bands, but not all (obviously it can't, since it didn't save the data).

Any thoughts on how I can have multiple dropdowns to add more than one band to an event? There are way too many bands to have checkboxes - and I hate the multi-select boxes - too difficult for most users.

Any help is GREATLY appreciated.

Dave
  • 28,833
  • 23
  • 113
  • 183

2 Answers2

0

You need to use different name attributes for each Band input and also set the structure up correctly for saving a HABTM relationship. Assuming that you've pushed the band list into $bands.

echo $this->Form->input('Band.Band.0', array('multiple'=>false, 'empty'=>true, 'options'=>$bands));
echo $this->Form->input('Band.Band.1', array('multiple'=>false, 'empty'=>true, 'options'=>$bands));
echo $this->Form->input('Band.Band.2', array('multiple'=>false, 'empty'=>true, 'options'=>$bands));

This should produce the correct structure when the user submits the form in that $this->data[Band][Band] is an array of band ids.

When you load an existing event for editing, you need to iterate through the bands and explicitly set a default value:

foreach($this->data['Band']['Band'] as $index => $band_id) {
    echo $this->Form->input("Band.Band.$index", array('multiple'=>false, 'empty'=>true, 'options'=>$bands, 'value'=>$band_id)));
}
Tyler
  • 2,126
  • 1
  • 12
  • 11
  • I'm not sure what you mean by "set the structure up correctly for saving HABTM relationship". I have the HABTM set up correctly in the model, and tried changing my inputs to that, but now it's not saving any of them. Any thoughts? – Dave May 01 '11 at 21:36
0

Thanks to Tyler for leading me in the right direction.

I ended up getting it to work with the following code.

The controller had nothing special, and saved the data in the HABTM connection table "bands_events" by using the normal $this->Event->save($this->data) ... just like if I wasn't using an array of bands.

If there's something in this code that's off, please let me know - I'm no Cake guru, I just tweaked Tylers code for 3+ hours until I got the syntax..etc that worked.

    $numBands = sizeof($this->data['Band']);
    if($numBands) {
        for($i=0; $i<$numBands; $i++) {
            echo $this->Form->input('Band.Band['.$i.']', 
                array('multiple'=>false, 'empty'=>true, 'options'=>$bands, 
                'value'=>$this->data['Band'][$i]['BandsEvent']['band_id']));
        }
    } else {
        echo $this->Form->input('Band.Band[0]', array('multiple'=>false,
            'empty'=>true, 'options'=>$bands));
    }
Dave
  • 28,833
  • 23
  • 113
  • 183