0

I’m having an issue saving HABTM data in a controller.

I have an Article model, which is associated to an Event model, as articles may be about specific events in my application. I’ve read the CakePHP cookbook entry on saving HABTM data so understand how it works on the controller level, but having difficulties understanding how it should look on the view side of things.

What I have in my form are inputs for the article data (title, excerpt, content etc). I then want checkboxes for each event in my database then I can then check to associate an article with the corresponding event(s).

Normally, I would have a checkbox like this:

<input type="checkbox" name="event[]" value="1" />

Where 1 is the ID of a venue, and do a checkbox like this for every event in my database, but this doesn’t seem to work in CakePHP.

What should I put for my field name when creating checkboxes with $this->Form->input()? What’s the “CakePHP” way?

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Can you be a little bit more specific about your form? Maybe you should provide your form view. – Cris Sawfish Sep 02 '13 at 10:53
  • I have an array of all events in my database. I want to generate a checkbox for each event so that I can save the HABTM relation when I submit the form, but I don’t know what format CakePHP is expecting I put for the field name in my `$this->Form->input()` call. – Martin Bean Sep 02 '13 at 10:57

2 Answers2

1

Assuming you've set the events in the controller as described in the Cook Book:

$this->set('events', $this->Event->find('list'));

then your input should look like this, basically like the example in the Cook Book, with the addition of the multiple option with an value of checkbox:

$this->Form->input('Event', array('multiple' => 'checkbox'));

This should give you a correctly formatted list of checkboxes with the events displayField as labels.

Your next problem might be validation, so also have a look at CakePHP HABTM data not saving to database

Community
  • 1
  • 1
ndm
  • 59,784
  • 9
  • 71
  • 110
0

I suppose that you save your data from the ArticlesController::add().

So your view, you should have something like the following

add.ctp

<?php
echo $this->Form->create('Article');
echo $this->Form->input('Article.title', array('type' => 'text'));
echo $this->Form->input('Article.excerpt', array('type' => 'textarea'));
echo $this->Form->input('Article.title', array('type' => 'textarea'));

/** Generate multiple checkboxes, assumung that your array of events is $events **/
foreach($events as $index => $evt) {
    echo $this->Form->input('Event'.$index.'.id', array('type' => 'checkbox', 'value' => $evt['Event']['id']));
}
echo $this->Form->submit('save');
?>

The foreach loop will create a set of checkboxes which looks like this

<input type="checkbox" name="data[Event][0][id] value="1">
<input type="checkbox" name="data[Event][1][id] value="2">
<input type="checkbox" name="data[Event][2][id] value="3">
...

The respected data sent to the controller can be accessed via the $this->request->data['Event'] array. If you have setup the relations correclty then the data saving is been done automatically for you.

I hope this helps.

Cris Sawfish
  • 335
  • 1
  • 7